你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
适用对象:
MongoDB vCore
投影 $meta
运算符用于在查询结果中包含元数据。 它可用于在输出文档中包括元数据,例如文本搜索分数或其他计算值。
语法
使用投影运算符的 $meta
语法如下所示:
db.collection.find( {$text: { $search: <string> } },
{ field: { $meta: <metaDataKeyword> }
}
)
参数
说明 | |
---|---|
field |
输出文档中包含元数据的字段的名称。 |
metaDataKeyword |
要包含常见关键字的元数据类型,例如 textScore 文本搜索分数。 |
示例
下面是演示投影运算符用法 $meta
的示例。
示例 1:包括文本搜索分数
我们有一个命名 stores
的集合,我们希望在文本搜索查询的结果中包含文本搜索分数。
db.stores.createIndex({ "name": "text"});
db.stores.find(
{ $text: { $search: "Equipment Furniture Finds" } },
{ _id: 1, name: 1, score: { $meta: "textScore" } }
).sort({ score: { $meta: "textScore" } }).limit(2)
此查询从 stores
集合中返回与文本搜索条件匹配的文档,并包含 score
包含文本搜索分数的字段。
{
"_id": "7a9aa41e-95bd-43c1-96cd-bcff0c3c33fb",
"name": "Fabrikam, Inc",
"score": 2
},
{
"_id": "ee51cc4c-6770-4bb7-bb61-cd0cc44cb387",
"name": "Proseware, Inc",
"score": 2
}
限制
- 如果未使用索引,则 { $meta: “indexKey” } 不返回任何内容。