On this page本页内容
$meta¶$meta¶Returns the metadata associated with a document, e.g. 返回与文档关联的元数据,例如执行文本搜索时的"textScore" when performing text search."textScore"。
A $meta expression has the following syntax:$meta表达式语法如下所示:
The $meta expression can specify the following values as the <metaDataKeyword>:$meta表达式可以将以下值指定为<metaDataKeyword>:
"textScore" |
|
"indexKey" |
|
MongoDB Atlas Search provides additional $meta keywords, such as:
Refer to the Atlas Search documentation for details.有关详细信息,请参阅Atlas搜索文档。
$meta: "textScore"¶Requires $text Search
|
|
|
|
|
|
|
Usage
|
|
|
$meta: "textScore"¶Create an 使用以下文档创建articles collection with the following documents:articles集合:
Create a text index on the 在title field:title字段上创建文本索引:
The following aggregation operation performs a text search and uses the 以下聚合操作执行文本搜索,并使用$meta operator to group by the text search score:$meta运算符根据文本搜索分数进行分组:
The operation returns the following results:操作返回以下结果:
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运算符来包括分配给每个匹配文档的分数:
The operation returns the following documents with the text score:该操作返回以下带有文本分数的文档:
For additional examples of 有关"textScore" projections and sorts, see Text Search Score Examples."textScore"投影和排序的其他示例,请参阅文本搜索分数示例。
$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集合:
Create the following compound index on the 在type and item fields:type和item字段上创建以下复合索引:
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运算符包含匹配文档的索引键值:
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:
The operation returns the matching documents with their corresponding index key:该操作返回匹配文档及其相应的索引键:
If no index is used, the { $meta: "indexKey" } does not return anything.
For example, the following operation does not use an index since no index exists on the 例如,以下操作不使用索引,因为price field to support the match condition:price字段上不存在支持匹配条件的索引:
For example, the following operation does not use an index since no index exists on the 例如,以下操作不使用索引,因为price field to support the match condition:price字段上不存在支持匹配条件的索引:
The operation returns the matching documents without the 该操作返回匹配的文档,但不包含idxKey field:idxKey字段: