对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

equals (MongoDB搜索操作符)

equals

equals 操作符检查字段是否与您指定的值匹配。equals 支持查询以下数据类型:

您可以使用 equals操作符查询数组中的布尔值、objectId、数字、日期和字符串(作为 token 类型进行索引)。如果大量中的至少一个元素与 equals操作符中的“值”字段匹配, MongoDB Search 就会将该文档添加到结果设立。

注意

equals 操作符支持最多 15 位十进制数字。文档或查询中的额外十进制数字可能会导致精度问题或查询不准确。

equals 通过以下语法实现:

{ $search: { "index": <index name>, // optional, defaults to "default" "equals": { "path": "<field-to-search>", "value": <boolean-value>|<objectId>|<number>|<date>|<string>, "score": <score-options>, "doesNotAffect": "<facet-to-exclude>" | [<array-of-facets>] } }}

equals 使用以下词条来构造查询:

字段
类型
说明
必需?

path

字符串

要搜索的索引字段。

是

value

布尔值、objectId、数字、日期、string、uuid 或 null。

要查询的值。

是

score

对象

分配给匹配搜索词结果的分数。使用以下选项之一修改分数:

  • boost将生成的分数乘以给定数字。

  • constant将结果分数替换为给定数字。

  • function:使用给定的表达式替换结果分数。

有关在查询中使用 score 的信息,请参阅对结果中的文档进行评分。

no

doesNotAffect

字符串或字符串数组

要从基于此查询的计数重新计算中排除的分面(Facet)或分面(Facet)列表。值必须是 $search.facets 或 $searchMeta.facets 中定义的分面(Facet)的一个或多个名称。该操作符符使用指定的分面来筛选文档以匹配此查询,但不会重新计算它们的计数。有关更多信息,请参阅多选分面(Facet)。

no

默认情况下,equals 使用 constant 评分。每个匹配文档的得分为 1。

查询大量值时,如果大量中的更多值与查询匹配, MongoDB Search 会分配更高的分数。

有关评分示例,请参阅示例部分。

以下示例使用了一个示例 users 集合。

users 集合包含以下三个文档:

db.users.insertMany([ { "_id" : ObjectId("5ed698faa1199b471010d70c"), "name" : "Jim Hall", "verified_user" : true, "account" : { "new_user" : true, "active_user" : true }, "teammates" : [ ObjectId("5ed6990aa1199b471010d70d"), ObjectId("59b99dbdcfa9a34dcd7885c8") ], "region" : "East", "account_created" : ISODate("2021-12-12T10:18:27.000+00:00"), "employee_number" : 257, "uuid" : UUID("fac32260-b511-4c69-8485-a2be5b7dda9e"), "job_title": "engineer" }, { "_id" : ObjectId("5ed6990aa1199b471010d70d"), "name" : "Ellen Smith", "verified_user" : true, "account" : { "new_user" : false, "active_user" : true }, "teammates" : [ ObjectId("5a9427648b0beebeb69537a5"), ObjectId("59b99dbdcfa9a34dcd7881d1") ], "region" : "Southwest", "account_created" : ISODate("2022-05-04T05:01:08.000+00:00"), "employee_number" : 258, "job_title": null }, { "_id" : ObjectId("5ed6994fa1199b471010d70e"), "name" : "Fred Osgood", "verified_user" : false, "account" : { "new_user" : false, "active_user" : false }, "teammates" : [ ObjectId("5a9427648b0beebeb69589a1"), ObjectId("59b99dbdcfa9a34dcd7897d3") ], "region" : "Northwest", "account_created" : ISODate("2022-01-19T08:22:15.000+00:00"), "employee_number" : 259, "job_title": null }])

users 集合使用以下索引定义进行索引:

{ "mappings": { "dynamic": true, "fields": { "name": { "type": "token", "normalizer": "lowercase" }, "region": [ { "type": "string" }, { "type": "token" } ] } }}

该索引定义指定以下内容:

  • 自动对所有可动态索引的字段进行索引。

  • 将 name 字段作为 token 类型进行索引,以支持使用 equals 操作符搜索字符串。

  • 将 region 字段建立为 string 类型的索引以支持字符串搜索,同时建立为 token 类型的索引以支持 facet 搜索。

要学习;了解如何创建MongoDB搜索索引,请参阅管理MongoDB搜索索引。

以下示例使用 equals 操作符在 users 集合中搜索 verified_user 字段设置为 true 的文档。

db.users.aggregate([ { "$search": { "equals": { "path": "verified_user", "value": true } } }, { "$project": { "name": 1, "_id": 0, "score": { "$meta": "searchScore" } } }])

以上查询返回以下结果:

{ "name" : "Jim Hall", "score" : 1 }{ "name" : "Ellen Smith", "score" : 1 }

“Jim Hall”和“Ellen Smith”的文档得分均为 1,因为这些文档将 verified_user 字段设置为 true。

以下示例使用 equals 操作符在 users 集合中搜索包含布尔值 true 的 account.new_user 字段的文档。

db.users.aggregate([ { "$search": { "equals": { "path": "account.new_user", "value": true } } }])

前面的查询返回“Jim Hall”的文档,因为该文档在 account 对象中包含 "new_user": true。

以下示例使用 equals 操作符在 users 集合中搜索 teammates 字段包含 ObjectId("5a9427648b0beebeb69589a1") 值的文档。

db.users.aggregate([ { "$search": { "equals": { "path": "teammates", "value": ObjectId("5a9427648b0beebeb69589a1") } } }])

前面的查询返回“Fred Osgood”的文档,因为该文档在 teammates 数组中包含 ObjectId("5a9427648b0beebeb69589a1")。

以下示例使用 equals 操作符在 users 集合中搜索 account_created字段包含与 ISODate("2022-05-04T05:01:08.000+00:00") 匹配的值的文档。

db.users.aggregate([ { "$search": { "equals": { "path": "account_created", "value": ISODate("2022-05-04T05:01:08.000+00:00") } } }])

前面的查询返回“Ellen Smith”的文档,因为该文档包含 "account_created": 2022-05-04T05:01:08.000+00:00。

以下示例使用 equals 操作符在 users 集合中搜索 employee_number字段包含与 259 匹配的值的文档。

db.users.aggregate([ { "$search": { "equals": { "path": "employee_number", "value": 259 } } }])

前面的查询会为 "Fred Osgood" 返回文档,因为该文档包含 "employee_number": 259。

以下示例使用 equals 操作符在 users 集合中搜索 name字段包含与 Jim Hall 匹配的值的文档。

db.users.aggregate([ { "$search": { "equals": { "path": "name", "value": "jim hall" } } }])

前面的查询返回“Jim Hall”的文档,因为该文档的name字段中包含 Jim Hall。MongoDB Search 将小写查询与文档中的大写值进行匹配,因为它将术语规范化为小写,如 索引定义 中指定的 name字段。

以下示例使用 equals 操作符在 users 集合中搜索 uuid字段包含与特定 UUID 匹配的值的文档。

db.users.aggregate([ { "$search": { "equals": { "path": "uuid", "value": UUID("fac32260-b511-4c69-8485-a2be5b7dda9e") } } }])

前面的查询返回“Jim Hall”的文档,因为该文档在 uuid 字段中包含指定的 UUID。

以下示例使用 equals 操作符在 users 集合中搜索 job_title 字段包含 null 值的文档。

db.users.aggregate([ { $search: { "equals": { "path": "job_title", "value": null } } }])

前面的查询会返回"Ellen Smith" 和 Fred Osgood 的文档,因为该文档的 job_title 字段中包含空值。

以下示例将 compound 操作符与 must、mustNot 和 equals 结合使用,以搜索 region 字段为 Southwest 并且 verified_user 字段不是 false 的文档。

db.users.aggregate([ { "$search": { "compound": { "must": { "text": { "path": "region", "query": "Southwest" } }, "mustNot": { "equals": { "path": "verified_user", "value": false } } } } }])

上面的查询返回“Ellen Smith”的文档,这是集合中唯一满足搜索条件的文档。

下面的示例查询具有以下搜索条件:

  • verified_user 字段必须设置为 true

  • 必须满足以下条件之一:

    • teammates 数组包含 ObjectId("5ed6990aa1199b471010d70d") 值

    • region 字段设置为 Northwest

db.users.aggregate([ { "$search": { "compound": { "must": { "equals": { "path": "verified_user", "value": true } }, "should": [ { "equals": { "path": "teammates", "value": ObjectId("5ed6990aa1199b471010d70d") } }, { "text": { "path": "region", "query": "Northwest" } } ], "minimumShouldMatch": 1 } } }, { "$project": { "name": 1, "_id": 0, "score": { "$meta": "searchScore" } } }])

以上查询返回以下结果:

{ "name" : "Jim Hall", "score" : 2 }

“Jim Hall”的文档得分为 2,因为它满足 must 子句以及两个 should 子句中的第一个子句的要求。

您可以使用复合查询搜索多个 ObjectID。以下示例查询使用 compound 操作符和 should 子句搜索三个不同的 ObjectID,其中的至少两个 ObjectID 必须满足查询条件。

db.users.aggregate([ { "$search": { "compound": { "should": [ { "equals": { "path": "teammates", "value": ObjectId("5a9427648b0beebeb69537a5") } }, { "equals": { "path": "teammates", "value": ObjectId("59b99dbdcfa9a34dcd7881d1") } }, { "equals": { "path": "teammates", "value": ObjectId("5a9427648b0beebeb69579d0") } } ], "minimumShouldMatch": 2 } } }, { "$project": { "name": 1, "_id": 0, "score": { "$meta": "searchScore" } } }])

以上查询返回以下结果:

{ "name" : "Ellen Smith", "score" : 2 }

“Ellen Smith”的文档得分为 2,因为它在 teammates 数组中包含两个指定的 ObjectID。

以下查询使用 $searchMeta 阶段来检索 users 集合中 verified_users 为真的区域数量。

db.users.aggregate([ { "$searchMeta": { "facet": { "operator": { "equals": { "path": "verified_user", "value": true } }, "facets": { "regionFacet": { "type": "string", "path": "region" } } } } }])

以下查询使用 $$SEARCH_META 聚合变量返回元数据和搜索结果,以检索 users 集合中 verified_users 为 true 的区域数量。

db.users.aggregate([ { "$search": { "facet": { "operator": { "equals": { "path": "verified_user", "value": true } }, "facets": { "regionFacet": { "type": "string", "path": "region" } } } } }, { "$limit": 2 }, { "$facet": { "docs": [ { "$project": { "type": 1, "description": 1 } } ], "meta": [ {"$replaceWith": "$$SEARCH_META"}, {"$limit": 1} ] } }, { "$set": { "meta": { "$arrayElemAt": ["$meta", 0] } } }])