Docs Menu
Docs Home
/
Start 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

  • A MongoDB account. See Sign Up for a MongoDB Account.

  • An Atlas cluster. See Create a Cluster.

  • Sample datasets loaded into your cluster.

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

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

  3. If the Clusters page is not already displayed, click Database in the sidebar.

2
sample-data-cluster
click to enlarge
3
1

Click the Atlas Search tab.

2

Click Create Index.

3

Select the Visual Editor configuration method, then click Next.

4
5

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

6

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

7

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

8

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.

9

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

10

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

4
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 $searchMeta stage.

4

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.

5

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

Next

Start with Guides