Querying with Facets
Overview
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
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
In Atlas, go to the Clusters page for your project.
If it's not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.
If it's not already displayed, select your desired project from the Projects menu in the navigation bar.
If it's not already displayed, click Clusters in the sidebar.
The Clusters page displays.
Create a search index with facets.
Go to the Atlas Search page for your cluster.
Click the cluster's name.
Click the Atlas Search tab.
The Atlas Search page displays.
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.
Run a faceted query on your index by using the Aggregation Pipeline.
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 apurchaseMethod
value ofIn store
.The facet named
locationFacet
that creates a bucket for each differentstoreLocation
value.
Review the results of the faceted query.
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.
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.