定义
$meta返回与文档关联的元数据,例如执行文本搜索时返回
"textScore"。A
$metaexpression has the following syntax:{ $meta: <metaDataKeyword> } The
$metaexpression can specify the following values as the<metaDataKeyword>:Keyword说明"textScore""indexKey"如果使用非文本索引,则返回文档的索引键。
{ $meta: "indexKey" }表达式仅用于调试目的,不用于应用程序逻辑,并且比cursor.returnKey()更受青睐。MongoDB Search provides additional
$metakeywords, such as:有关详细信息,请参阅MongoDB搜索文档。
重要
Stable API V1 不支持以下
$meta关键字:"textScore""indexKey""score""searchScore""scoreDetails""searchHighlights""searchSequenceToken""searchScoreDetails""searchRootDocumentId""vectorSearchScore"
You can use
searchScoreonly in$searchqueries andvectorSearchScoreonly in$vectorSearchqueries. If you usesearchScoreandvectorSearchScorein any other query, that query will fail.You can use
searchRootDocumentIdonly in$searchqueries withreturnScope.
行为
文本分数元数据 $meta: "textScore"
需要 $text 搜索
{ $meta: "textScore" }表达式必须与$text结合使用。例如:在查找中,必须在查询谓词中指定
$text操作符,才能使用{ $meta: "textScore" }。如果未在查询谓词中指定$text操作符,则操作失败。
注意
$text为自管理(非 Atlas)部署提供文本查询功能。对于MongoDB上托管的数据, MongoDB还提供改进的全文查询解决方案MongoDB Search。
可用性
投影中的使用
按文本分数筛选
聚合时,在输出文本分数值字段的阶段之后,可以在后续阶段指定查询条件或对字段进行操作。例如,请参阅聚合管道中的 $text。
在查找中,不能对文本得分指定查询条件。请改用聚合。
排序使用
"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" }表达式仅用于调试目的,不适用于应用程序逻辑。{ $meta: "indexKey" }表达式优先于cursor.returnKey()。
可用性
聚合时,
{ $meta: "indexKey" }表达式可以包含在接受聚合表达式的各个阶段,例如$project、$group、$sortByCount等,但不包含$sort。但是,使用聚合管道,可以先投影{ $meta: "indexKey" }表达式(例如在$project、$addFields等中),然后在后续$sort阶段按该字段排序。在查找中,
{ $meta: "indexKey" }表达式只能作为投影文档的一部分使用。
返回值
返回的值取决于数据库在索引中表示值的方式,并且可能会因版本而异。所表示的值可能不是该字段的实际值。
返回的值取决于系统选择的执行计划。例如,如果有两个可能用于回答查询的索引,那么“indexKey”元数据的值取决于选择了哪个索引。
如果未使用索引,则
{ $meta: "indexKey" }表达式不会返回值,并且该字段不会作为输出的一部分。