$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从MongoDB 4.4开始,必须与$text
query.$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 overcursor.returnKey()
.{ $meta: "indexKey" }
表达式仅用于调试目的,不用于应用程序逻辑,并且比cursor.returnKey()
更受欢迎。New in version 4.4.4.4版新增。MongoDB Atlas Search provides additionalMongoDB Atlas Search提供了额外的$meta
keywords, such as:$meta
键,例如:Refer to the Atlas Search documentation for details.有关详细信息,请参阅Atlas Search文档。ImportantThe following以下$meta
keywords are not supported in Stable API V1:$meta
键在Stable API V1中不受支持:"textScore"
"indexKey"
"searchScore"
"searchHighlights"
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从MongoDB 4.4开始,如果您没有在查询谓词中指定$text
operator in the query predicate, the operation fails.$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 insort()
.{ $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" }
表达式可以是投影文档的一部分,以包含文本分数元数据。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.在查找中,不能对文本分数指定查询条件。请改用聚合。
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" }
表达式可以用作排序操作的一部分,以根据文本分数元数据进行排序;即。,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 thetextScore
.{$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 thetextScore
.{ $meta: "textScore" }
对结果文档进行排序,而不必投影textScore
。
In MongoDB 4.2 and earlier, to use在MongoDB 4.2及更早版本中,要将{ $meta:"textScore" }
expression withsort()
for a find operation, you must also include the same expression in the projection.{ $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在查找中,从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.查询系统将忽略排序中的字段名。
In MongoDB 4.2 and earlier, to include the在MongoDB 4.2及更早版本中,要在查找操作的投影和排序中都包含{ $meta: "textScore" }
in both the projection and sort for a find operation, you must specify the same field name in both places.{ $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 overcursor.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
. However, 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
、$group
$sortByCount
等,但不能包含在$sort
中。但是,使用聚合管道,您可以首先投影{ $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."textScore"
投影和排序的其他示例,请参阅文本搜索分数示例。
$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"
}