In this exercise, you’ll update the $sort
stage so the documents are sorted by the ratio field, rather than the state field. You’ll also specify that the documents be sorted in descending order. You’ll then add a field to the pipeline that limits the results to the five top documents, based on how they’re sorted in the $sort
stage.
To limit the number of returned documents
- Go to the 8: $sort tab in the Aggregation Editor.
- Replace the existing expression with the following code:
{ ratio: -1 }
The new code specifies that the documents should be ordered based on the ratio field. The value -1
indicates that they should be sorted in descending order.
- In the Stage Input pane, click the Execute button. Studio 3T runs the pipeline for all stages up to but not including the
$sort
stage. - In the Stage Output pane, click the Execute button. This will execute the pipeline up to and including the
$sort
stage. The following figure shows part of the results in Table View, with the documents now sorted by the ratio field.
- Ensure that the 8: $sort tab still active. 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.
- On the new tab, select the $limit option from the Operator drop-down list.
- In the editor window, delete the placeholder text and type 5. You do not need to type anything else. This tells Studio 3T to return only the top five documents when running the aggregation. The returned documents will be based on how the documents are sorted when inputted into the
$limit
stage. - In the Stage Input pane, click the Execute button. Studio 3T runs the pipeline for all stages up to but not including the
$limit
stage. - In the Stage Output pane, click the Execute button. This will execute the pipeline up to and including the
$limit
stage. The following figure shows the results, as they appear in Table View.
The pipeline now returns only five documents, those with the highest ratio
values. Notice that the state with the highest ratio is the District of Columbia (Washington DC), which is not actually a state but is often classified as one in data sets such as this.
- 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 returns the five documents with the highest transaction-to-population ratio. Notice that the $project
stage is excluded from the pipeline.
- On the Pipeline tab, right-click the
$project
stage, and then click Delete Selected Stage. - Click Save on the Aggregation Editor toolbar to save your changes.
- Close the Aggregation tab and then close Studio 3T.