Querying with Facets
Overview
In this guide, you will learn how to run a query with the facet operator.
Time required: 15 minutes
What You'll Need
A MongoDB account. See Sign Up for a MongoDB Account.
An Atlas cluster. See Create a Cluster.
Sample datasets loaded into your cluster.
Procedure
Navigate to the Database Deployments page for your project.
Click the Collections tab.

Create a search index with facets.
In the right-hand pane, click sample_supplies
,
then click the sales
collection.
Click Search Indexes in the upper right of the collection preview pane.
Click Create Search Index.
Click the Visual Editor configuration method, then click Next.
Review the Database and Collection list to
ensure that sample_supplies.sales
is selected, then click
Next.
Run a query on your index using the Aggregation Pipeline.
Click the Aggregation tab.
If you do not see a pipeline with an initial stage window in the right pane, click Create New and select Pipeline.
In the Aggregation pane, click on the dropdown menu labeled Select... and select the $searchMeta stage.
Replace the placeholder code with the following
compound
operator:
{ "facet": { "operator": { "text": { "query": ["In store"], "path": "purchaseMethod" } }, "facets": { "locationFacet": { "type": "string", "path": "storeLocation", } } } }
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
.
Summary
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.
Congrats. You’ve completed all the guides. Want to take the next step? Register for the developer exam.