Docs Menu

Docs HomeStart with Guides

Querying with the Compound Operator

In this guide, you will learn how to use multiple search operators in a compound query using the aggregation pipeline.

Time required: 15 minutes


Click the Collections tab.


In the right-hand pane, click sample_supplies, then click the sales collection.


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.


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.

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.

What's Next
Querying with Facets
15 mins

Retrieve documents in MongoDB with counts of specific values.

Start Guide
Chapter 3
Atlas Search
  • Build a Dynamic Index
  • Build an Index with Static Field Mappings
  • Querying with the Compound Operator
  • Querying with Facets
Share Feedback
© 2023 MongoDB, Inc.


  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2023 MongoDB, Inc.