On this page
$meta (aggregation)
在本页面
Definition
$meta
- 2.6 版的新功能。
返回管道操作中与文档关联的元数据,例如"textScore"
在执行文本搜索时。
$meta表达式具有以下语法:
{ $meta: <metaDataKeyword> }
$meta表达式可以将以下关键字指定为<metaDataKeyword>
:
Keyword | Description | Sort Order |
---|---|---|
"textScore" |
返回与每个匹配文档的相应$text查询关联的分数。Literals 分数表示文档与搜索字词匹配的程度。如果未与$text查询一起使用,则返回空值。 | Descending |
Behavior
{ $meta: "textScore" }
表达式是$sort阶段接受的唯一expression。
尽管可以在管道中接受表达式的任何地方使用{ $meta: "textScore" }
表达式,但仅在包括$match阶段和$text查询的管道中才有意义。
Views不支持文本搜索。
Examples
考虑包含以下文档的articles
集合:
{ "_id" : 1, "title" : "cakes and ale" }
{ "_id" : 2, "title" : "more cakes" }
{ "_id" : 3, "title" : "bread" }
{ "_id" : 4, "title" : "some cakes" }
以下聚合操作执行文本搜索,并使用$meta运算符对文本搜索分数进行分组:
db.articles.aggregate(
[
{ $match: { $text: { $search: "cake" } } },
{ $group: { _id: { $meta: "textScore" }, count: { $sum: 1 } } }
]
)
该操作返回以下结果:
{ "_id" : 0.75, "count" : 1 }
{ "_id" : 1, "count" : 2 }
有关更多示例,请参见聚合管道中的文本搜索。