Docs 菜单

使用分面进行查询

在本指南中,您将学习;了解如何使用分分面(Facet)收集器运行查询,对查询结果进行群组,并返回每个组的计数。

所需的时间:10 分钟

1

警告

Navigation Improvements In Progress

We're currently rolling out a new and improved navigation experience. If the following steps don't match your view in the Atlas UI, see the preview Atlas documentation.

  1. 如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含所需项目的组织。

  2. 如果尚未显示,请从导航栏的Projects菜单中选择所需的项目。

  3. 如果尚未出现,请单击侧边栏中的 Clusters(集群)。

    会显示集群页面。

2
点击放大
3
1

警告

Navigation Improvements In Progress

We're currently rolling out a new and improved navigation experience. If the following steps don't match your view in the Atlas UI, see the preview Atlas documentation.

  1. 单击集群的名称。

  2. 单击 Atlas Search 标签页。

    将显示 Atlas Search 页面。

2

单击 Create Index(连接)。

3

选择Visual Editor配置方法,然后单击Next

4
5

对于Database and Collection ,选择sample_supplies数据库和sales集合。 然后,单击Next

6

单击Refine Your Index 。 在Index Configuration窗格中,关闭Dynamic Mapping

7

Field Mappings窗格中,单击Add Field Mapping 。 在模态窗口中,从Field Name下拉菜单中选择purchaseMethod

8

Data Type下拉菜单中选择String 。 单击模式窗口底部的Add 。 保持Index Configurations值不变,然后单击模式窗口底部的Add

9

Field Mappings窗格中,单击Add Field Mapping 。 在模态窗口中,从Field Name下拉菜单中选择storeLocation

10

Data Type下拉列表中选择StringFacet 。 单击模式窗口底部的Add

4
1

单击 Aggregation 标签页。

2

单击+ Add Stage开始创建聚合管道。

3

在阶段窗口中,单击标有Select...的下拉菜单并选择$searchMeta阶段。

4

将占位符代码替换为以下查询。

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

此查询将facet收集器与以下方式结合使用:

  • text操作符仅对purchaseMethod值为In store的文档进行计数。

  • 名为locationFacet的分面,用于为每个不同的storeLocation值创建一个存储桶。

5

结果应类似于以下示例文档(部分展开):

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

您可以通过单击展开ArrayObject字段以查看其内容。

如果您成功完成了本指南中的步骤,那么您就已经创建了带有分面的 Atlas Search 索引,并创建了一个聚合管道,该管道利用分面将匹配记录组织到类别中,以提高可见性。

接下来的步骤
成为 MongoDB 专业人士

恭喜。 您已完成所有指南。 想要执行下一步? 报名参加开发者考试。

了解详情