定义
equalsequals操作符检查字段是否与您指定的值匹配。equals支持查询以下数据类型:number,包括
int32、int64和doublenull
您可以使用
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 使用以下词条来构造查询:
字段 | 类型 | 说明 | 必需? |
|---|---|---|---|
| 字符串 | 要搜索的索引字段。 | 是 |
| 要查询的值。 | 是 | |
| 对象 | 分配给匹配搜索词结果的分数。使用以下选项之一修改分数:
有关在查询中使用 | no |
| 字符串或字符串数组 | 要从基于此查询的计数重新计算中排除的分面(Facet)或分面(Facet)列表。值必须是 | no |
评分行为
默认情况下,equals 使用 constant 评分。每个匹配文档的得分为 1。
查询大量值时,如果大量中的更多值与查询匹配, MongoDB Search 会分配更高的分数。
See the Examples section for scoring examples.
示例
以下示例使用了一个示例 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 } ])
{ "_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 } } } ])
The preceding query returns the document for "Jim Hall" because that document contains "new_user": true in the account object.
ObjectId 示例
以下示例使用 equals 操作符在 users 集合中搜索 teammates 字段包含 ObjectId("5a9427648b0beebeb69589a1") 值的文档。
db.users.aggregate([ { "$search": { "equals": { "path": "teammates", "value": ObjectId("5a9427648b0beebeb69589a1") } } } ])
The preceding query returns the document for "Fred Osgood" because that document contains ObjectId("5a9427648b0beebeb69589a1") in the teammates array.
日期示例
以下示例使用 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") } } } ])
The preceding query returns the document for "Ellen Smith" because that document contains "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 } } } ])
The preceding query returns the document for "Fred Osgood" because that document contains "employee_number": 259.
字符串示例
以下示例使用 equals 操作符在 users 集合中搜索 name字段包含与 Jim Hall 匹配的值的文档。
db.users.aggregate([ { "$search": { "equals": { "path": "name", "value": "jim hall" } } } ])
The preceding query returns the document for "Jim Hall" because that document contains Jim Hall in the name field. MongoDB Search matches the lowercase query to the uppercase value in the document because it normalizes the term to lowercase as specified in the index definition for the name field.
UUID 示例
以下示例使用 equals 操作符在 users 集合中搜索 uuid字段包含与特定 UUID 匹配的值的文档。
db.users.aggregate([ { "$search": { "equals": { "path": "uuid", "value": UUID("fac32260-b511-4c69-8485-a2be5b7dda9e") } } } ])
The preceding query returns the document for "Jim Hall" because that document contains the specified UUID in the uuid field.
空示例
以下示例使用 equals 操作符在 users 集合中搜索 job_title 字段包含 null 值的文档。
db.users.aggregate([ { $search: { "equals": { "path": "job_title", "value": null } } } ])
The preceding query returns the documents for "Ellen Smith" and Fred Osgood because that documents contain the null value in the job_title field.
复合示例查询
以下示例将 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" } } } } } ])
[ { count: { lowerBound: Long('2') }, facet: { regionFacet: { buckets: [ { _id: 'East', count: Long('1') }, { _id: 'Southwest', count: Long('1') } ] } } } ]
以下查询使用 $$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] } } } ])
[ { docs: [ { _id: ObjectId('5ed698faa1199b471010d70c'), name: 'Jim Hall', region: 'East' }, { _id: ObjectId('5ed6990aa1199b471010d70d'), name: 'Ellen Smith', region: 'Southwest' } ], meta: { count: { lowerBound: Long('2') }, facet: { regionFacet: { buckets: [ { _id: 'East', count: Long('1') }, { _id: 'Southwest', count: Long('1') } ] } } } } ]