In this exercise, you’ll import the states_transactions.js file into the Aggregation Editor. The file contains the aggregate
statement that you built in the first section of this course. You’ll use this statement as the foundation for all four exercises in this section, modifying the statement as you work through the last three exercises.
To import an aggregate
statement into the Aggregation Editor
- Launch Studio 3T and connect to MongoDB Atlas.
- In the Connection Tree, expand the sales database node and, if necessary, expand the Collections node.
- Right-click the customers collection node, and then click Open Aggregation Editor. Studio 3T adds the Aggregation tab to the main window. The tab displays the Aggregation Editor, with the editor’s Pipeline tab active. At this point, the aggregation pipeline is empty.
- On the Aggregation Editor toolbar, click the Open button (folder icon) on the left side, navigate to the folder that contains the states_transactions.js file, and double-click the file. Studio 3T uses the aggregate statement in that file to automatically populate the pipeline, as shown in the following figure.
If you did not generate the states_transactions.js file in the first section, you can instead use the clipboard feature in the Aggregation Editor to import the statement. To do so, copy the following aggregate
statement to your clipboard and then paste it into the Aggregation Editor by clicking the Clipboard button at the right end of the Aggregation Editor toolbar:
db.customers.aggregate( [ { "$match": { "dob": { "$lt": ISODate("1970-01-01T00:00:00.000Z") } } }, { "$group": { "_id": "$address.state", "total": { "$sum": "$transactions" } } }, { "$sort": { "total": -1 } } ], { "allowDiskUse": true, "collation": { "locale": "en_US" } } );
Regardless of which approach you take to importing the statement, the Pipeline tab will list the aggregate
statement’s three stages. Each stage listing includes the stage’s operator and its expression:
- The first stage is based on the
$match
operator, which filters out all documents except those with adob
value prior to 1970. - The second stage is based on the
$group
operator, which groups data by theaddress.state
field and finds the total number of transactions for each state. - The third stage is based on the
$sort
operator, which orders the data by thetotal
field, in descending order.
- Go to the tab for the first stage. Because this stage is based on the
$match
operator, the tab is named 1: $match. The tab also shows the stage’s operator in the Operator drop-down list, and it provides an editor window (text box) for working with the operator’s expression. - In the Stage Input pane near the bottom of the tab, click the Execute button (green right arrow). Studio 3T executes the pipeline up to but not including the current stage. This allows you to see what data is being inputted into this stage before it’s applied to the pipeline. In this case, the current stage is the first stage, so the input data includes all documents in the collection.
- In the Stage Output pane, click the Execute button. Studio 3T executes the pipeline up to and including the current stage, which filters the data based on the
dob
field. The following figure shows the results in both panes. The initial pipeline input includes 1,000 documents, but the pipeline output for this stage includes only 406 documents.
The Stage Input and Stage Output panes let you compare data before a stage runs and after it runs, making it easier to build and troubleshoot each stage within the aggregation pipeline, without impacting the other stages.
- Repeat steps 6 and 7 for the
$group
stage and$sort
stage, noting how the input and output result sets compare. - Go to the Query Code tab and review the
aggregate
statement, as shown in the following figure.
Notice that the statement includes the three pipeline stages and two statement options: allowDiskUse
and collation
. The options were specified as part of the original aggregate statement that you imported into the Aggregation Editor. For information about the options, refer to the first section in this course.
- Go to the Options tab, which is shown in the following figure.
The options that were specified in the original aggregate statement are reflected here as well. The Allow disk use option is selected, and the collation is set to en_US.
- Go to the Pipeline tab and click the Execute button on the Aggregation Editor toolbar. Studio 3T runs the statement and returns the results to the Pipeline output pane at the bottom of the tab, as shown in the following figure.
As expected, the pipeline groups the data by state and returns the total number of transactions per state. The data is sorted in descending order based on the values in the total
field. These are the same results you’d receive if you ran the aggregate
statement in IntelliShell.
- Leave the Aggregation tab open and the existing statement in place for the next exercise. You’ll be building on this statement by adding a new pipeline stage.