$meta

On this page本页内容

Definition定义

$meta

Returns the metadata associated with a document, e.g. "textScore" when performing text search.返回与文档关联的元数据,例如执行文本搜索时的"textScore"

A $meta expression has the following syntax:$meta表达式语法如下:

{ $meta: <metaDataKeyword> }

The $meta expression can specify the following values as the <metaDataKeyword>:$meta表达式可以将以下值指定为<metaDataKeyword>

Keyword关键字Description描述
"textScore"

Returns the score associated with the corresponding $text query for each matching document. 返回与每个匹配文档的相应$text查询关联的分数。The text score signifies how well the document matched the search term or terms.文本分数表示文档与搜索词的匹配程度。

Starting in MongoDB 4.4, must be used in conjunction with a $text query.从MongoDB 4.4开始,必须与$text查询结合使用。

In earlier versions, if not used in conjunction with a $text query, returns a score of null.在早期版本中,如果不与$text查询一起使用,则返回分数null

"indexKey"

Returns an index key for the document if a non-text index is used. 如果使用非文本索引,则返回文档的索引键。The { $meta: "indexKey" } expression is for debugging purposes only, and not for application logic, and is preferred over cursor.returnKey().{ $meta: "indexKey" }表达式仅用于调试目的,而不是用于应用程序逻辑,它比cursor.returnKey()更受欢迎。

New in version 4.4.在版本4.4中新增

MongoDB Atlas Search provides additional $meta keywords, such as:提供了额外的$meta关键字,例如:

Refer to the Atlas Search documentation for details.有关详细信息,请参阅Atlas Search文档。

Behavior行为

Text Score Metadata 文本分数元数据$meta: "textScore"

Requires $text Search需要$text搜索

  • The { $meta: "textScore" } expression must be used in conjunction with $text search. { $meta: "textScore" }表达式必须与$text搜索结合使用。For example:例如:

    • In aggregation, you must specify a $match stage with a $text query in the pipeline to use the {$meta: "textScore" } expression in later stage(s). 在聚合中,必须在管道中指定一个带有$text查询的$match阶段,以便在以后的阶段中使用{$meta: "textScore" }表达式。If you do not specify the $text query in the $match stage, the operation fails.如果未在$match阶段指定$text查询,操作将失败。
    • In find, you must specify the $text operator in the query predicate to use { $meta: "textScore" }. 在查找中,必须在查询谓词中指定$text运算符才能使用{ $meta: "textScore" }Starting in MongoDB 4.4, if you do not specify the $text operator in the query predicate, the operation fails.从MongoDB 4.4开始,如果未在查询谓词中指定$text运算符,操作将失败。

Availability可用性

  • In aggregation, the { $meta: "textScore" } expression can be included in various stages that accept aggregation expressions, such as $project, $group, $sort, etc.在聚合中,{ $meta: "textScore" }表达式可以包含在接受聚合表达式的各个阶段中,例如$project$group$sort等。
  • In find, the { $meta: "textScore" } expression can be included in projection and in sort().在查找中,{ $meta: "textScore" }表达式可以包含在投影sort()中。

Usage In Projection投影中的用法

  • The { $meta: "textScore" } expression can be a part of the projection document to include the text score metadata.{ $meta: "textScore" }表达式可以是投影文档的一部分,以包含文本score元数据。
  • The $meta expression can be present in either an inclusion or an exclusion projection.$meta表达式可以出现在包含或排除投影中。
  • If you set the expression to a field name that already exists in the document, the projected metadata value overwrites the existing value.如果将表达式设置为文档中已存在的字段名,则投影的元数据值将覆盖现有值。

Filter on Text Score根据文本分数筛选

  • In aggregation, following a stage that outputs a field with the text score value, you can specify a query condition or operate on the field in subsequent stages. 在聚合中,在输出具有文本分数值的字段的阶段之后,可以指定查询条件或在后续阶段对字段进行操作。For example, see Text Search in the Aggregation Pipeline.例如,请参阅聚合管道中的文本搜索
  • In find, you cannot specify a query condition on the text score. Use aggregation instead.在find中,不能对文本分数指定查询条件。改用聚合。

Usage In Sort排序中的用法

  • The { $meta: "textScore" } expression can be used as a part of a sort operation to sort by the text score metadata; i.e.,{ $meta: "textScore" }表达式可以用作排序操作的一部分,以根据文本得分元数据进行排序;即,

    • In aggregation, $sort stage.在聚合中,$sort阶段。
    • In find, sort() method.在查找中,sort()方法。
  • The "textScore" metadata sorts in descending order."textScore"元数据按降序排序。
  • To use in a sort operation, set the { $meta: "textScore" } expression to an arbitrary field name. 要在排序操作中使用,请将{ $meta: "textScore" }表达式设置为任意字段名。The field name is disregarded by the query system.查询系统将忽略字段名。

Sort without Projection不带投影排序

  • In aggregation, you can sort the resulting documents by { $meta: "textScore" } without also having to project the textScore.在聚合中,您可以按{ $meta: "textScore" }对结果文档进行排序,而无需投影textScore
  • In find, starting in MongoDB 4.4, you can sort the resulting documents by { $meta: "textScore" } without also having to project the textScore.在查找中,从MongoDB 4.4开始,您可以按{ $meta: "textScore" }对结果文档进行排序,而无需投影textScore

    In MongoDB 4.2 and earlier, to use { $meta: "textScore" } expression with sort() for a find operation, you must also include the same expression in the projection.在MongoDB4.2及更早版本中,要将{ $meta: "textScore" }表达式与sort()一起用于查找操作,还必须在投影中包含相同的表达式。

Sort with Projection使用投影排序

  • 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. 在聚合中,如果在投影和排序中都包含{ $meta: "textScore" }表达式,则投影和排序可以为表达式使用不同的字段名。The field name in the sort is disregarded by the query system.查询系统将忽略排序中的字段名。
  • In find, starting in MongoDB 4.4, 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. 在find中,从MongoDB 4.4开始,如果在投影和排序中都包含{ $meta: "textScore" }表达式,则投影和排序可以为表达式使用不同的字段名。The field name in the sort is disregarded by the query system.查询系统将忽略排序中的字段名。

    In MongoDB 4.2 and earlier, to include the { $meta: "textScore" } in both the projection and sort for a find operation, you must specify the same field name in both places.在MongoDB4.2及更早版本中,要在查找操作的投影和排序中同时包含{ $meta: "textScore" },必须在这两个位置指定相同的字段名。

Index Key Metadata $meta: "indexKey" (Aggregation and Find)索引键元数据$meta:“indexKey”(聚合和查找)

Usage用法

  • The { $meta: "indexKey" } expression is for debugging purposes only and not for application logic.{ $meta: "indexKey" }表达式仅用于调试目的,不用于应用程序逻辑。
  • The { $meta: "indexKey" } expression is preferred over cursor.returnKey().{ $meta: "indexKey" }表达式优先于cursor.returnKey()

Availability可用性

  • In aggregation, the { $meta: "indexKey" } expression can be included in various stages that accept aggregation expressions, such as $project, $group$sortByCount, etc., but not $sort. 在聚合中{ $meta: "indexKey" }表达式可以包含在接受聚合表达式的各个阶段中,例如$project$group$sortByCount等,但不包括$sortHowever, with an aggregation pipeline, you can first project the { $meta: "indexKey" } expression (such as in a $project, $addFields, etc. ) and then, sort by that field in a subsequent $sort stage.然而,使用聚合管道,您可以首先投影{ $meta: "indexKey" }表达式(例如在$project$addFields等中),然后在随后的$sort阶段按该字段排序。
  • In find, the { $meta: "indexKey" } expression is only available as part of the projection document.在查找中{ $meta: "indexKey" }表达式仅作为投影文档的一部分可用。

Return Value返回值

  • The value returned depends on how the database decides to represent values in an index and may change across versions. 返回的值取决于数据库决定如何在索引中表示值,并且可能会在不同版本之间发生变化。The represented value may not be the actual value for the field.表示的值可能不是字段的实际值。
  • The value returned depends on the execution plan chosen by the system. 返回的值取决于系统选择的执行计划。For example, if there are two possible indexes which can be used to answer the query, then the value of the "indexKey" metadata depends on which index is selected.例如,如果有两个可能的索引可用于回答查询,则“indexKey”元数据的值取决于选择的索引。
  • If an index is not used, the { $meta: "indexKey" } expression does not return a value and the field is not included as part of the output.如果未使用索引,{ $meta: "indexKey" }表达式不会返回值,并且该字段不包含在输出中。

Examples示例

$meta: "textScore"

Create an articles collection with the following documents:使用以下文档创建articles集合:

db.articles.insertMany([
   { "_id" : 1, "title" : "cakes and ale" },
   { "_id" : 2, "title" : "more cakes" },
   { "_id" : 3, "title" : "bread" },
   { "_id" : 4, "title" : "some cakes" },
   { "_id" : 5, "title" : "two cakes to go" },
   { "_id" : 6, "title" : "pie" }
])

Create a text index on the title field:title字段上创建文本索引

db.articles.createIndex( { title: "text"} )

The following aggregation operation performs a text search and uses the $meta operator to group by the text search score:以下聚合操作执行文本搜索,并使用$meta运算符按文本搜索得分进行分组:

db.articles.aggregate(
   [
     { $match: { $text: { $search: "cake" } } },
     { $group: { _id: { $meta: "textScore" }, count: { $sum: 1 } } }
   ]
)

The operation returns the following results:该操作返回以下结果:

{ "_id" : 0.75, "count" : 1 }
{ "_id" : 0.6666666666666666, "count" : 1 }
{ "_id" : 1, "count" : 2 }

For more examples, see Text Search in the Aggregation Pipeline.有关更多示例,请参阅聚合管道中的文本搜索

The following query performs a text search for the term cake and uses the $meta operator in the projection document to include the score assigned to each matching document:以下查询对术语cake执行文本搜索,并在投影文档中使用$meta运算符来包括分配给每个匹配文档的分数:

db.articles.find(
   { $text: { $search: "cake" } },
   { score: { $meta: "textScore" } }
)

The operation returns the following documents with the text score:该操作返回以下带有文本分数的文档:

{ "_id" : 4, "title" : "some cakes", "score" : 1 }
{ "_id" : 1, "title" : "cakes and ale", "score" : 0.75 }
{ "_id" : 5, "title" : "two cakes to go", "score" : 0.6666666666666666 }
{ "_id" : 2, "title" : "more cakes", "score" : 1 }

For additional examples of "textScore" projections and sorts, see Text Search Score Examples.

$meta: "indexKey"

Note注意

The { $meta: "indexKey" } expression is for debugging purposes only and not for application logic. { $meta: "indexKey" }表达式仅用于调试目的,不用于应用程序逻辑。MongoDB returns the value associated with the index chosen by the query system. MongoDB返回与查询系统选择的索引关联的值。The system can choose a different index upon subsequent execution.系统可以在后续执行时选择不同的索引。

For the selected index, the value returned depends on how the database decides to represent values in an index and may change across versions. 对于所选索引,返回的值取决于数据库决定如何在索引中表示值,并且可能会在不同版本之间发生变化。The represented value may not be the actual value for the field.表示的值可能不是字段的实际值。

Create an orders collection with the following documents:使用以下文档创建orders集合:

db.orders.insertMany([
   { "item" : "abc", "price" : NumberDecimal("12"), "quantity" : 2, "type": "apparel" },
   { "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : 1, "type": "electronics" },
   { "item" : "abc", "price" : NumberDecimal("10"), "quantity" : 5, "type": "apparel" }
])

Create the following compound index on the type and item fields:typeitem字段上创建以下复合索引:

db.orders.createIndex( { type: 1, item: 1 } )

The following aggregation operation finds all documents with type equal to apparel and uses the $meta operator to include the index key value for the matching document if an index was used:以下聚合操作查找type等于apparel(服装)的所有文档,如果使用了索引,则使用$meta运算符来包括匹配文档的索引键值:

db.orders.aggregate(
   [
     { $match: { type: "apparel" } },
     { $addFields: { idxKey: { $meta: "indexKey" } } }
   ]
)

The following operation finds all documents with type equal to apparel and uses the $meta operator to include the index key value for the matching document if an index was used:以下操作查找type等于apparel(服装)的所有文档,如果使用了索引,则使用$meta运算符来包括匹配文档的索引键值:

db.orders.find( { type: "apparel" }, { idxKey: { $meta: "indexKey" } } )

The operation returns the matching documents with their corresponding index key:该操作将返回匹配的文档及其相应的索引键:

{
   "_id" : ObjectId("5e98a33ceaf5e9dcf2b8dcde"),
   "item" : "abc",
   "price" : NumberDecimal("12"),
   "quantity" : 2,
   "type" : "apparel",
   "idxKey" : { "type" : "apparel", "item" : "abc" }
}
{
   "_id" : ObjectId("5e98a33ceaf5e9dcf2b8dce0"),
   "item" : "abc",
   "price" : NumberDecimal("10"),
   "quantity" : 5,
   "type" : "apparel",
   "idxKey" : { "type" : "apparel", "item" : "abc" }
}

If no index is used, the { $meta: "indexKey" } does not return anything.如果没有使用索引,{ $meta: "indexKey" }不会返回任何内容。

For example, the following operation does not use an index since no index exists on the price field to support the match condition:例如,以下操作不使用索引,因为price字段上不存在支持匹配条件的索引:

db.orders.aggregate(
   [
     { $match: { price: { $gte: NumberDecimal("10") } } },
     { $addFields: { idxKey: { $meta: "indexKey" } } }
   ]
)

For example, the following operation does not use an index since no index exists on the price field to support the match condition:例如,以下操作不使用索引,因为price字段上不存在支持匹配条件的索引:

db.orders.find(
   { price: { $gte: NumberDecimal("10") } },
   { idxKey: { $meta: "indexKey" } }
)

The operation returns the matching documents without the idxKey field:该操作返回不带idxKey字段的匹配文档:

{
   "_id" : ObjectId("5e98a33ceaf5e9dcf2b8dcde"),
   "item" : "abc",
   "price" : NumberDecimal("12"),
   "quantity" : 2,
   "type" : "apparel"
}
{
   "_id" : ObjectId("5e98a33ceaf5e9dcf2b8dcdf"),
   "item" : "jkl",
   "price" : NumberDecimal("20"),
   "quantity" : 1,
   "type" : "electronics"
}
{
   "_id" : ObjectId("5e98a33ceaf5e9dcf2b8dce0"),
   "item" : "abc",
   "price" : NumberDecimal("10"),
   "quantity" : 5,
   "type" : "apparel"
}
←  $mergeObjects (aggregation)$min (aggregation) →