Docs Menu
Docs Home
/
Start with Guides

Querying with the Compound Operator

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

Time required: 5 minutes

  • 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. To create a default dynamic index, see Build a Dynamic Index.

1
  1. If it's not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.

  2. If it's not already displayed, select your desired project from the Projects menu in the navigation bar.

  3. If it's not already displayed, click Clusters in the sidebar.

    The Clusters page displays.

2
sample-data-cluster
click to enlarge
3

Click the Collections tab.

sample-data-tab
click to enlarge
4

Click the sample_supplies database, then click the sales collection.

5
1

Click the Aggregation tab.

2

Click + Add Stage to begin creating your aggregation 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 query.

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

This query uses the compound operator with:

  • The filter clause to return only documents with a purchaseMethod of Online.

  • The should clause to return documents with an items field that contains notepad to have a higher score than those without.

5

In the right-hand pane, your results should include a list of documents that resemble the following sample document:

items: Array
0: Object
1: Object
2: Object
name: "notepad"
...
storeLocation: "Denver"
customer: Object
couponUsed: false
purchaseMethod: "Online"
_id: ObjectID('5bd761dcae323e45a93cd06e')
saleDate: 2013-11-22T18:49:45.212+00:00

Note

The fields don't always appear in the same order for every returned document.

You can expand Array and Object fields to view their contents by clicking on them.

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
10 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