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

1
2
sample-data-cluster
3

Click the Collections tab.

sample-data-tab
4

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

5
1

Click the Aggregation tab.

2

If you do not see a pipeline with an initial stage window in the right pane, click Create New and select Pipeline.

3

In the stage window, click on the dropdown menu labeled Select... and select the $search stage.

4

Replace the placeholder code with the following compound operator:

{
"compound": {
"filter": [{
"text": {
"query": "Online",
"path": "purchaseMethod"
}
}],
"should": [{
"text": {
"query": "notepad",
"path": "items"
}
}]
}
}
5

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.

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.

About

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