Docs Menu

Docs HomeStart with Guides

Querying with Facets

In this guide, you will learn how to run a query with the facet collector to group your query results and returns the count for each of these groups.

Time required: 10 minutes


Click Create Index.


Select the Visual Editor configuration method, then click Next.


For the Database and Collection, select the sample_supplies database and the sales collection. Then, click Next.


Click Refine Your Index. In the Index Configuration pane, toggle off Dynamic Mapping.


In the Field Mappings pane, click Add Field Mapping. In the modal window, select purchaseMethod from the Field Name dropdown menu.


From the Data Type dropdown menu, select String. Click Add at the bottom of the modal window. Leave the Index Configurations values unchanged, and click Add at the bottom of the modal window.


In the Field Mappings pane, click Add Field Mapping. In the modal window, select storeLocation from the Field Name dropdown menu.


From the Data Type dropdown, select StringFacet. Click Add at the bottom of the modal window.


Click the Aggregation tab.


Click + Add Stage to begin creating your aggregation pipeline.


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


Replace the placeholder code with the following query.

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

This query uses the facet collector with:

  • The text operator to count only documents with a purchaseMethod value of In store.

  • The facet named locationFacet that creates a bucket for each different storeLocation value.


Your results should resemble the following sample document, which 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

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


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