On this page本页内容
$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>:
"textScore" |
|
"indexKey" |
|
MongoDB Atlas Search provides additional 提供了额外的$meta keywords, such as:$meta关键字,例如:
Refer to the Atlas Search documentation for details.有关详细信息,请参阅Atlas Search文档。
$meta: "textScore"$text搜索The { $meta: "textScore" } expression must be used in conjunction with $text search. { $meta: "textScore" }表达式必须与$text搜索结合使用。For example:例如:
$match stage with a $text query in the pipeline to use the {$meta: "textScore" } expression in later stage(s). $text查询的$match阶段,以便在以后的阶段中使用{$meta: "textScore" }表达式。$text query in the $match stage, the operation fails.$match阶段指定$text查询,操作将失败。$text operator in the query predicate to use { $meta: "textScore" }. $text运算符才能使用{ $meta: "textScore" }。$text operator in the query predicate, the operation fails.$text运算符,操作将失败。{ $meta: "textScore" } expression can be included in various stages that accept aggregation expressions, such as $project, $group, $sort, etc.{ $meta: "textScore" }表达式可以包含在接受聚合表达式的各个阶段中,例如$project、$group、$sort等。{ $meta: "textScore" } expression can be included in projection and in sort().{ $meta: "textScore" }表达式可以包含在投影和sort()中。{ $meta: "textScore" } expression can be a part of the projection document to include the text score metadata.{ $meta: "textScore" }表达式可以是投影文档的一部分,以包含文本score元数据。$meta expression can be present in either an inclusion or an exclusion projection.$meta表达式可以出现在包含或排除投影中。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" }表达式可以用作排序操作的一部分,以根据文本得分元数据进行排序;即,
"textScore" metadata sorts in descending order."textScore"元数据按降序排序。{ $meta: "textScore" } expression to an arbitrary field name. { $meta: "textScore" }表达式设置为任意字段名。{ $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 在查找中,从MongoDB 4.4开始,您可以按{ $meta: "textScore" } without also having to project the textScore.{ $meta: "textScore" }对结果文档进行排序,而无需投影textScore。
{ $meta: "textScore" } expression with sort() for a find operation, you must also include the same expression in the projection.{ $meta: "textScore" }表达式与sort()一起用于查找操作,还必须在投影中包含相同的表达式。{ $meta: "textScore" } expression in both the projection and sort, the projection and sort can have different field names for the expression. { $meta: "textScore" }表达式,则投影和排序可以为表达式使用不同的字段名。In find, starting in MongoDB 4.4, if you include the 在find中,从MongoDB 4.4开始,如果在投影和排序中都包含{ $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.查询系统将忽略排序中的字段名。
{ $meta: "textScore" } in both the projection and sort for a find operation, you must specify the same field name in both places.{ $meta: "textScore" },必须在这两个位置指定相同的字段名。$meta:“indexKey”(聚合和查找){ $meta: "indexKey" } expression is for debugging purposes only and not for application logic.{ $meta: "indexKey" }表达式仅用于调试目的,不用于应用程序逻辑。{ $meta: "indexKey" } expression is preferred over cursor.returnKey().{ $meta: "indexKey" }表达式优先于cursor.returnKey()。{ $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等,但不包括$sort。{ $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阶段按该字段排序。{ $meta: "indexKey" } expression is only available as part of the projection document.{ $meta: "indexKey" }表达式仅作为投影文档的一部分可用。{ $meta: "indexKey" } expression does not return a value and the field is not included as part of the output.{ $meta: "indexKey" }表达式不会返回值,并且该字段不包含在输出中。$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"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:type和item字段上创建以下复合索引:
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"
}