In this exercise, you’ll use the Aggregation Editor to create an aggregate statement and add the first stage, which will filter the collection’s documents based on the specified conditions. To match the correct documents, the stage will use the $or
and the $and
Boolean expression operators, as well as the $in
array expression operator.
To use expression operators to filter input documents
- 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 Add button (large plus sign). The Aggregation Editor adds a tab to the right of the Pipeline tab and makes the new tab active. The tab is named 1: $match because it is the first stage and because Studio 3T uses
$match
as its default operator when creating a new stage. You’ll be working with the$match
operator for this exercise. - In the editor window, delete the placeholder text and curly braces with the following code:
{ $and: [ { interests: { $in: [ "Software" ] } }, { $or: [ { interests: { $in: [ "Web Design" ] } }, { package: "Premium" } ] } ] }
The expression is similar to the one you saw in the introductory material. The $and
operator defines two conditions. Both conditions must both evaluate to true
for a document to be included in the stage’s output. The first condition uses the $in
operator to verify whether the interests array includes the term Software
. If the term exists, the condition will evaluate to true
.
The second condition starts with the $or
operator, which defines its own two conditions. The first of these uses the $in
operator to check whether the interests array includes the term Web Design
. The second condition verifies whether the package value equals Premium
. If the array meets either of these two conditions, the second condition of the $and
operator evaluates to true
.
If both the $and
conditions evaluate to true
, the document will be included in the stage’s output.
- In the Stage Input pane, click the Execute button. Studio 3T executes the pipeline up to but not including the current stage and returns the data to that pane. This is the data that is used as input for the current stage.
- In the StageOutput pane, click the Execute button. This will execute the pipeline up to and including the
$match
stage. The following figure shows part of the results, as they appear in Table View.
Because this is the first stage in the pipeline, the input data includes all 1,000 documents in the customers
collection. However, the output data includes only 37 documents. These are the documents that meet both conditions defined by the $and
operator.
- Click Save on the Aggregation Editor toolbar.
- When the Save Aggregation Script dialog box appears, name the script count_interests.js, navigate to the folder where you want to save the script, and click Save.
- Leave the Aggregation tab open and the existing statement in place for the next exercise.