对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

$meta(表达式操作符)

$meta

返回与文档关联的元数据,例如执行文本搜索时返回 "textScore"

A $meta expression has the following syntax:

{ $meta: <metaDataKeyword> }

The $meta expression can specify the following values as the <metaDataKeyword>:

Keyword
说明

"textScore"

返回与每份匹配文档相应的 $text 查询关联的得分。文本得分表示文档与搜索词的匹配程度。

{ $meta: "textScore" } 必须与 $text 查询结合使用。

在早期版本中,如果不与 $text 查询结合使用,返回的分数为空值。

"indexKey"

如果使用非文本索引,则返回文档的索引键。{ $meta: "indexKey" }表达式仅用于调试目的,不用于应用程序逻辑,并且比 cursor.returnKey() 更受青睐。

MongoDB Search provides additional $meta keywords, such as:

有关详细信息,请参阅MongoDB搜索文档。

重要

Stable API V1 不支持以下 $meta 关键字:

  • "textScore"

  • "indexKey"

  • "score"

  • "searchScore"

  • "scoreDetails"

  • "searchHighlights"

  • "searchSequenceToken"

  • "searchScoreDetails"

  • "searchRootDocumentId"

  • "vectorSearchScore"

You can use searchScore only in $search queries and vectorSearchScore only in $vectorSearch queries. If you use searchScore and vectorSearchScore in any other query, that query will fail.

You can use searchRootDocumentId only in $search queries with returnScope.

  • { $meta: "textScore" } 表达式必须与 $text 结合使用。例如:

    • 聚合时,必须在管道中指定具有 $text 查询的 $match 阶段,才能在后续阶段中使用 { $meta: "textScore" } 表达式。如果未在 $match 阶段指定 $text 查询,则操作将失败。

    • 在查找中,必须在查询谓词中指定 $text 操作符,才能使用 { $meta: "textScore" }。如果未在查询谓词中指定 $text 操作符,则操作失败。

    注意

    $text 为自管理(非 Atlas)部署提供文本查询功能。对于MongoDB上托管的数据, MongoDB还提供改进的全文查询解决方案MongoDB Search。

  • 聚合时,{ $meta: "textScore" } 表达式可以包含在接受聚合表达式的各个阶段中,例如 $project$group$sort 等。

  • 在查找中,{ $meta: "textScore" } 表达式可以包含在投影sort() 中。

  • { $meta: "textScore" } 表达式可以作为投影文档的一部分,以包含文本得分元数据。

  • The $meta expression can be present in either an inclusion or an exclusion projection.

  • 如果将表达式设置为文档中已存在的字段名称,则投影的元数据值将覆盖现有值。

  • 聚合时,在输出文本分数值字段的阶段之后,可以在后续阶段指定查询条件或对字段进行操作。例如,请参阅聚合管道中的 $text

  • 在查找中,不能对文本得分指定查询条件。请改用聚合。

  • { $meta: "textScore" } 表达式可用作排序操作的一部分,以按文本得分元数据进行排序;也即

    • 聚合时,$sort 阶段。

    • 查找时,sort() 方法。

  • "textScore" 元数据按降序排序。

  • 要在排序操作中使用,可将 { $meta: "textScore" } 表达式设置为任意字段名称。查询系统会忽略字段名称。

  • 聚合时,可以按 { $meta: "textScore" } 对结果文档进行排序,而无需投影 textScore

  • 在查找中,您可以按 { $meta: "textScore" } 对结果文档进行排序,而无需投影 textScore

  • In aggregation, if you include the { $meta: "textScore" } expression in both the projection and sort, the projection and sort can have different field names for the expression. The field name in the sort is disregarded by the query system.

  • In find, if you include the { $meta: "textScore" } expression in both the projection and sort, the projection and sort can have different field names for the expression. The field name in the sort is disregarded by the query system.

  • { $meta: "indexKey" } 表达式仅用于调试目的,不适用于应用程序逻辑。

  • { $meta: "indexKey" } 表达式优先于 cursor.returnKey()

  • 聚合时{ $meta: "indexKey" } 表达式可以包含在接受聚合表达式的各个阶段,例如 $project$group$sortByCount 等,但不包含 $sort。但是,使用聚合管道,可以先投影 { $meta: "indexKey" } 表达式(例如在 $project$addFields 等中),然后在后续 $sort 阶段按该字段排序。

  • 在查找中{ $meta: "indexKey" } 表达式只能作为投影文档的一部分使用。

  • 返回的值取决于数据库在索引中表示值的方式,并且可能会因版本而异。所表示的值可能不是该字段的实际值。

  • 返回的值取决于系统选择的执行计划。例如,如果有两个可能用于回答查询的索引,那么“indexKey”元数据的值取决于选择了哪个索引。

  • 如果使用索引,则 { $meta: "indexKey" } 表达式不会返回值,并且该字段不会作为输出的一部分。

给本页内容打分

在此页面上