Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
MongoDB Branding Shape
Click here >
Docs 菜单

$searchMeta

$searchMeta

The $searchMeta stage returns different types of metadata result documents.

注意

To run $searchMeta queries over sharded collections, your cluster must run MongoDB v6.0 or later.

$searchMeta 阶段在以下环境中可用:

$searchMeta 管道阶段采用以下原型形式:

{
$searchMeta: {
"index": "<index-name>",
"<collector-name>"|"<operator-name>": {
<collector-specification>|<operator-specification>
},
"concurrent": true | false,
"count": {
<count-options>
},
"returnScope": {
"path": "<embedded-documents-field-to-retrieve>"
}
"returnStoredSource": true | false
}
}

$searchMeta 阶段采用包含以下字段的文档:

字段
类型
必要性
说明

<collector-name>

对象

可选的

用于查询的收集器名称。您可以提供一个包含收集器特定选项的文档作为该字段的值。值必须为 facet 才能检索所定义分面(Facet)名称到该分面(Facet)的存储桶大量的映射。要学习;了解更多信息,请参阅 facet(MongoDB搜索操作符)。您必须指定此项或 <operator-name>

concurrent

布尔

Optional

专用搜索节点上并行搜索各分段。如果集群上没有单独的搜索节点, MongoDB Search 会忽略此标志。如果省略,则默认为 false。要学习;了解更多信息,请参阅跨分段并行查询执行。

count

对象

Optional

指定用于检索结果计数的计数选项的文档。要学习;了解更多信息,请参阅对MongoDB搜索结果进行计数。

index

字符串

Optional

要使用的MongoDB Search索引的名称。如果省略,则默认为 default

如果索引名称拼写错误或者指定的索引在集群上尚不存在,则MongoDB Search 不会返回结果。

<operator-name>

对象

可选的

用于搜索的操作符名称。您可以提供一个包含操作符特定选项的文档作为该字段的值。 您必须指定此项或 <collector-name>$searchMeta仅返回默认的count 元数据。

returnScope

对象

Optional

将查询上下文设置为指定的嵌入式文档字段的对象。如果您的集群 MongoDB 版本低于 8.2,您还必须指定 returnStoredSource 并将其设置为 true

returnStoredSource

布尔

可选的

标志,指定是对后端数据库执行完整文档查找,还是直接从 MongoDB Search 仅返回存储的源字段。如果省略,则默认值为 false。如果您指定 returnScope 且您的集群 MongoDB 版本低于 8.2,则必须为 true

要了解详情,请参阅返回已存储源字段

The $searchMeta stage must be the first stage in any pipeline.

The structure of the metadata results document that is returned by the $searchMeta stage varies based on the type of results. MongoDB Search supports the following result types:

类型
Result Structure(结果结构)

count

结果中包含的计数结果指示结果中返回的计数是搜索结果的总数还是下限。要了解详情,请参阅计数结果

facet

分面查询的结果是所定义的分面名称到该分面的存储桶数组的映射。要了解详情,请参阅分面结果

假设 sample_mflix.movies 集合上有如下索引。

{
"mappings": {
"dynamic": false,
"fields": {
"year": {
"type": "number"
}
}
}
}

The following query searches for the number of movies released in 1998 using the $searchMeta stage.

db.movies.aggregate([
{
"$searchMeta": {
"range": {
"path": "year",
"gte": 1998,
"lt": 1999
},
"count": {
"type": "total"
}
}
}
])
[ { count: { total: Long("552") } } ]

如果您在使用MongoDB Search $search 查询时遇到问题,请参阅查询故障排除。