Exercise 2: Grouping the documents in the aggregation pipeline

In this exercise, you will add the second stage to the pipeline. This stage is based on the $group aggregate operator, which lets you group the documents in the pipeline based on a specific field.

To group the documents in the aggregation pipeline

1. On the Pipeline tab of the Aggregation Editor, ensure that the Stage 1 row is selected (the $match row), click the small down arrow to the right of the Add button, and click Add New Stage After Selected Stage

The Aggregation Editor adds the Stage 2 tab to the right of the Stage 1 tab and makes the new tab active. The editor again selects the $match operator for the new stage.

Aggregation Editor

2. On the Stage 2 tab, select the $group option from the Filter drop-down list in the upper left corner.

The $group operator groups the pipeline documents by a specified expression—typically the name of a field.

When you select the $group option, the Aggregation Editor adds a set of curly braces to the editor to enclose the expression, along with the _id field and several placeholders, which are a mix of plain text and comments.

3. In the editor window, delete the _id field and all the placeholders and type the following expression:

_id: "$address.city", 
total: { $sum: "$transactions" }

The expression includes two arguments, separated by a comma. Each argument is its own expression. 

  • The first argument defines the _id field, which determines how the documents should be grouped.

In this case, the grouping is based on the values in the address.city field, which is represented by the field name, preceded by a dollar sign ($address.city).

  • The second argument defines how the grouped documents should be aggregated.

The first part of the argument species the name of the outputted column (total), which will contain the aggregated totals. 

The second part of this argument—enclosed in curly braces—specifies an accumulator operator ($sum), followed by the field on which the aggregation will be based ($transactions).

Together these elements specify that the query should return the total number of transactions per city.

4. In the Stage Input pane in the bottom section, click the Execute button. This will execute the pipeline up to but not including the second stage.

5. In the Stage Output pane in the bottom section, click the Execute button. This will execute the pipeline up to and including the second stage. The following figure shows both sets of results, as they appear in Table View. 

output view

The figure shows only part of the Stage 2 results (which total 37 documents), but you can see how they include the number of transactions for each city.

6. Go to the Query Code tab. Verify that the code includes the $group operator and its expression.

7. Go to the Pipeline tab. Verify that the pipeline includes the Stage 2 row and that the row contains the $group operator and its expression.

Leave the Aggregation tab and aggregate statement in place for the next exercise.

Log in

Log in to your Studio 3T Academy account

Forgot password?

Don't have an account yet? Sign up
Sign up

Start learning MongoDB today. All fields are mandatory.

By signing up for a course, you agree to the 3T Software Labs Privacy Policy.

Already have an account? Log in