패싯으로 쿼리하기
개요
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.
소요 시간: 10분
준비물
MongoDB 계정 MongoDB 계정 가입을 참조하세요.
Atlas 클러스터 클러스터 생성을 참조합니다.
클러스터에 로드된 샘플 데이터 세트입니다.
절차
Navigate to the Database Deployments page for your project.
Create a search index with facets.
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.
Aggregation 탭을 클릭합니다.
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.
요약
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.