In this exercise, you’ll add a stage to the pipeline after the $group
stage. The new stage will be based on the $project
aggregate operator, which lets you reshape the documents in the pipeline. For example, you can use the operator to filter out fields or add calculated fields. In this case, you’ll use the operator to exclude the _id
field from the pipeline and add a new field named state
that will contain the _id
values.
To replace a field in the aggregation pipeline
- On the Pipeline tab of the Aggregation Editor, ensure that the
$group
pipeline stage is selected. - On the Aggregation Editor toolbar, click the small down arrow to the right of the Add button (the large plus sign), and then click Add New Stage After Selected Stage.
The Aggregation Editor adds a new tab to the right of the 2: $group tab and makes the new tab active. Initially, the new tab is named 3: $match because Studio 3T uses$match
as its default operator when creating a new stage.
- On the new tab, select the $project operator from the Operator drop-down list near the tab’s upper left corner. The
$project
operator lets you specifically include and exclude fields in the pipeline.
When you select the $project operator, the Aggregation Editor changes the name of the tab to 3: $project and adds the initial curly braces and placeholder text.
- In the editor window, replace the placeholder text and curly braces with the following code:
{ _id: 0, state: "$_id", total: 1 }
The expression includes three arguments, separated by commas. Each argument is its own expression:
- The first argument specifies that the
_id
field should be suppressed, as indicated by the0
value. If you want to explicitly include a field, you must set the value to1
. - The second argument adds a new column named state and sets its values to the
_id
field. Conceptually, you’re replacing the original_id
field with thestate
field but retaining the same values. - The third argument specifies that the
total
column should also be included in the pipeline.
- In the Stage Input pane, click the Execute button. Studio 3T runs the pipeline for the first two stages and returns the data to that pane. This is the data that is used as input for the $project stage.
- In the Stage Output pane, click the Execute button. This will execute the pipeline up to and including the
$project
stage. The following figure shows part of the results, as they appear in Table View.
Notice that the _id
field is still included in the output data. This is because a MongoDB document always requires an _id
field. However, if you switch to JSON View, you can see that the results include only the total and state fields.
- Go to the Query Code tab and verify that the aggregate method now includes the
$project
operator and its expression. - Go to the Pipeline tab and verify that the pipeline includes the
$project
stage. - In the Pipeline output pane, click the Refresh button to update the results.
- Click Save on the Aggregation Editor toolbar to save your changes.
- Leave the Aggregation tab open and the existing statement in place for the next exercise. You’ll be building on this statement by adding another pipeline stage.