In this exercise, you’ll launch IntelliShell and create an aggregate
statement that includes only one stage. The stage is based on the $match
aggregate operator, which lets you filter the documents in the pipeline so you’re working with only a subset of documents in later stages.
In the exercises that follow this one, you’ll add two more stages to the aggregation pipeline, and you’ll add two processing options to the statement, after the pipeline. By the end of the exercises, you’ll have created a statement that returns the number of transactions in each state, as they are stored in the customers
collection.
To filter the documents in the aggregation pipeline
- Launch Studio 3T and connect to MongoDB Atlas.
- In the Connection Tree, right-click the connection (top-level node) and click Add Database.
- In the Add Database dialog box, type sales in the Database Name text box, and then click OK. Studio 3T adds the sales database node to the Connection Tree.
- Right-click the sales database node and click Import Collections.
- In the Import dialog box, select the JSON option if not selected, and then click Configure. Studio 3T adds the JSON Import tab to the main window.
On the JSON Import tab, you can add files that contain source data to be imported into the database. The files appear as a list in the tab’s main window, which you can then select for import.
- Click the Add Source Files button (the plus sign just above the main window).
- In the file manager dialog box, navigate to the folder where you saved the customers.json file.
- Select the file and click Open. Studio 3T adds the file to the import list.
- Select the customers.json file from the list, and then click the Execute button on the tab’s toolbar. When the Confirm import dialog box appears, click OK. Studio 3T adds the
customers
collection to thesales
database. - Close the JSON Import tab. If prompted to save your changes, click Discard changes.
- In the Connection Tree, expand the sales database node and, if necessary, expand the Collections node. The Connection Tree should now include the customers collection node.
- Right-click the customers collection node, and then click Open IntelliShell. Studio 3T adds the IntelliShell tab to the main window. By default, Studio 3T defines a basic find statement on the
customers
collection object, but you’ll be replacing this statement with anaggregate
statement. - Replace the find statement with the following
aggregate
statement:
db.customers.aggregate( [ { "$match": { "dob": { "$lt": ISODate("1970-01-01T00:00:00.000Z") } } } ] );
The statement calls the aggregate method on the customers collection. In this case, the aggregate method takes one argument—the pipeline. The pipeline is enclosed in square brackets and includes only one stage, which is enclosed in curly braces. The stage uses the $match operator to return those documents with a dob value before 1970. The top pane of the IntelliShell tab should now look similar to the following figure.
The operator’s expression specifies the dob
field, followed by a subexpression. The subexpression includes the $lt
(less than) comparison operator and a datetime value for January 1, 1970. The ISODate
constructor converts the string to a datetime object, making it possible to carry out the comparison.
- On the IntelliShell toolbar, click the Execute button (the green arrow with the screen tip that reads Execute entire script). Studio 3T runs the aggregate statement and displays the results in the tab’s lower pane, as shown in the following figure. The results are displayed in Table View.
The first stage should return 406 documents. If you can’t see the Count Documents button, check that the Enable Query Assist button is on in the Intellishell toolbar. If the pipeline contained a second stage, MongoDB would use these documents when processing that stage.
- Leave the IntelliShell tab open and the existing statement in place for the next exercise. You’ll be building on this statement by adding the next pipeline stage.