Docs Menu

Docs HomeStart with Guides

Querying with Facets

In this guide, you will learn how to run a query with the facet operator.

Time required: 15 minutes

1
2
sample-data-cluster
3

Click the Collections tab.

sample-data-tab
4
1

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

2

Click Search Indexes in the upper right of the collection preview pane.

3

Click Create Search Index.

4

Click the Visual Editor configuration method, then click Next.

5

Review the Database and Collection list to ensure that sample_supplies.sales is selected, then click Next.

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 Aggregation pane, click on the dropdown menu labeled Select... and select the $searchMeta stage.

4

Replace the placeholder code with the following compound operator:

{
"facet": {
"operator": {
"text": {
"query": ["In store"],
"path": "purchaseMethod"
}
},
"facets": {
"locationFacet": {
"type": "string",
"path": "storeLocation",
}
}
}
}
5

Review the results of the compound search in the right-hand pane. The following sample document is shown partially expanded.

count: Object
lowerBound: 2819
facet: Object
locationFacet: Object
buckets: Array
0: Object
id: "Denver"
count: 864
1: Object
id: "Seattle"
count: 648
2: Object
3: Object
4: Object
5: Object

The count field returns the number of records with a purchaseMethod value of In store. The facet field returns an array of buckets that show how many of the matching records belong to each storeLocation.

If you successfully completed the procedure in this guide, you have created an Atlas Search index with faceting, and created an aggregation pipeline that leverages faceting to organize matching records into categories for improved visibility.

What's Next
Become a MongoDB Professional

Congrats. You’ve completed all the guides. Want to take the next step? Register for the developer exam.

Learn More
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.