Querying with the Compound Operator
Overview
In this guide, you will learn how to use multiple search operators in a compound query using the aggregation pipeline.
Time required: 15 minutes
What You'll Need
A MongoDB account. See Sign Up for a MongoDB Account.
An Atlas cluster. See Create a Cluster.
Sample datasets loaded into your cluster.
A search index with default settings and dynamic indexing. See Build a Dynamic Index.
Procedure
Navigate to the Database Deployments page for your project.
Click the Collections tab.

In the right-hand pane, click sample_supplies
,
then click the sales
collection.
Run a query on your index using the Aggregation Pipeline.
Click the Aggregation tab.
If you do not see a pipeline with an initial stage window in the right pane, click Create New and select Pipeline.
In the stage window, click on the dropdown menu labeled Select... and select the $search stage.
Replace the placeholder code with the following
compound
operator:
{ "compound": { "filter": [{ "text": { "query": "Online", "path": "purchaseMethod" } }], "should": [{ "text": { "query": "notepad", "path": "items" } }] } }
Review the results of the compound search in the right-hand pane. Your results should include the fields listed in the following sample document.
Note
The fields will not always appear in the same order for every returned document.
items: Array storeLocation: "Denver" customer: Object couponUsed: false purchaseMethod: "Online" _id: ObjectID('5bd761dcae323e45a93cd06e') saleDate: 2013-11-22T18:49:45.212+00:00
You can expand Array
and Object
fields to view their
contents by clicking on them.
Due to the filter
operator, only records with a
purchaseMethod
value of Online
appear.
Additionally, due to the should
operator,
records with an items
value of notepad
score
higher than those without.
Summary
If you successfully completed the procedure in this guide, you have created an Atlas Search aggregation stage with a compound operator, and have seen how different clauses in the operator interact to focus your search results across multiple axes. In the next guide, you will learn how to group search results using facets.
Retrieve documents in MongoDB with counts of specific values.