定义
The in operator in MongoDB Search performs a search for an array of BSON number, date, boolean, objectId, uuid, or string values at the given path and returns documents where the value of the field equals any value in the specified array. If the field holds an array, then the in operator selects the documents whose field holds an array that contains at least one element that matches any value in the specified array.
语法
in操作符的语法如下:
{ $search: { "index": <index name>, // optional, defaults to "default" "in": { "path": "<field-to-search>", "score": <options>, "value": <single-or-array-of-values-to-search>, "doesNotAffect": "<facet-to-exclude>" | [<array-of-facets>] } } }
字段
字段 | 类型 | 说明 | 必要性 |
|---|---|---|---|
| 字符串或字符串数组 | 必需 | |
| 对象 | 分配给匹配搜索词结果的分数。使用以下选项之一修改分数:
| Optional |
| 必需 | ||
| 字符串或字符串数组 | 要从基于此查询的计数重新计算中排除的分面(Facet)或分面(Facet)列表。值必须是 | Optional |
示例
以下示例使用 in 操作符查询 sample_analytics.customers 集合中的集合。如果您在集群上加载示例数据并创建一个名为 default 的 MongoDB Search 索引,该索引使用集合上的静态映射,则可以对集合运行以下查询。
样本索引
示例索引定义指定了以下操作,以支持针对集合中索引字段的 in 操作员查询:
自动索引集合中所有可动态索引的字段。
将
name字段静态索引为词元类型,并将字段中的文本转换为小写。
{ "mappings": { "index": "default", "dynamic": true, "fields": { "name": { "normalizer": "lowercase", "type": "token" } } } }
要学习;了解如何创建MongoDB搜索索引,请参阅管理MongoDB搜索索引。
样本查询
以下查询使用 in 操作符在Atlas Search birthdate 字段(包含单个值)中查找出生在给定日期的客户。 该查询使用$project阶段执行以下操作:
省略结果中的
_id字段。在结果中仅包含
name和birthdate字段。
1 db.customers.aggregate([ 2 { 3 "$search": { 4 "in": { 5 "path": "birthdate", 6 "value": [ISODate("1977-03-02T02:20:31.000+00:00"), ISODate("1977-03-01T00:00:00.000+00:00"), ISODate("1977-05-06T21:57:35.000+00:00")] 7 } 8 } 9 }, 10 { 11 "$project": { 12 "_id": 0, 13 "name": 1, 14 "birthdate": 1 15 } 16 } 17 ])
1 [ 2 { 3 name: 'Elizabeth Ray', 4 birthdate: ISODate("1977-03-02T02:20:31.000Z") 5 }, 6 { 7 name: 'Brad Cardenas', 8 birthdate: ISODate("1977-05-06T21:57:35.000Z") 9 } 10 ]
MongoDB Search 会返回与查询中指定的日期相匹配的两个文档。
以下查询使用in操作符查询accounts字段,其中包含一个数字数组,查找帐号为371138 、 371139或371140的客户。 该查询使用$project阶段执行以下操作:
省略结果中的
_id字段。在结果中仅包含
name和accounts字段。
1 db.customers.aggregate([ 2 { 3 "$search": { 4 "in": { 5 "path": "accounts", 6 "value": [371138, 371139, 371140] 7 } 8 } 9 }, 10 { 11 "$project": { 12 "_id": 0, 13 "name": 1, 14 "accounts": 1 15 } 16 } 17 ])
1 [ 2 { 3 name: 'Elizabeth Ray', 4 accounts: [ 371138, 324287, 276528, 332179, 422649, 387979 ] 5 } 6 ]
MongoDB Search 仅返回一个与查询中指定的帐号 371138 匹配的文档。
以下查询使用text操作符查询name字段中名字为James的客户。 该查询使用in操作符为与_id字段中给定 objectId 关联的客户指定偏好。 查询使用$limit阶段将输出限制为5个结果,并使用$project阶段限制输出:
在结果中仅包含
_id和name字段。将名为
score的字段添加到结果中。
1 db.customers.aggregate([ 2 { 3 "$search": { 4 "compound": { 5 "must": [{ 6 "in": { 7 "path": "name", 8 "value": ["james sanchez", "jennifer lawrence"] 9 } 10 }], 11 "should": [{ 12 "in": { 13 "path": "_id", 14 "value": [ObjectId("5ca4bbcea2dd94ee58162a72"), ObjectId("5ca4bbcea2dd94ee58162a91")] 15 } 16 }] 17 } 18 } 19 }, 20 { 21 "$limit": 5 22 }, 23 { 24 "$project": { 25 "_id": 1, 26 "name": 1, 27 "score": { $meta: "searchScore" } 28 } 29 } 30 ])
1 [ 2 { 3 _id: ObjectId("5ca4bbcea2dd94ee58162a72"), 4 name: 'James Sanchez', 5 score: 2 6 }, 7 { 8 _id: ObjectId("5ca4bbcea2dd94ee58162a71"), 9 name: 'Jennifer Lawrence', 10 score: 1 11 } 12 ]
MongoDB Search 返回在 name字段中包含 James Sanchez 和 Jennifer Lawrence 的文档。MongoDB搜索对包含 name: 'James Sanchez' 的文档评分较高,因为它与 should 子句中指定的 ObjectId 匹配。
分面查询
以下查询使用 in 操作符在 active 字段中搜索包含布尔值的活跃客户。该查询返回出生日期属于以下区间的活跃客户数量:
1970-01-01,包括此存储桶的下限
1980-01-01,1970-01-01 存储桶的不含上限以及此存储桶的包含下限
1990-01-01,1980-01-01 存储桶的不含上限以及此存储桶的包含下限
2000-01-01,作为 1990-01-01 分段的排除性上界
1 db.customers.aggregate([ 2 { 3 "$searchMeta": { 4 "facet": { 5 "operator": { 6 "in": { 7 "path": "active", 8 "value": null 9 } 10 }, 11 "facets": { 12 "birthdateFacet": { 13 "type": "date", 14 "path": "birthdate", 15 "boundaries": [ISODate("1970-01-01"), ISODate("1980-01-01"), ISODate("1990-01-01"), ISODate("2000-01-01")], 16 "default": "other" 17 } 18 } 19 } 20 } 21 } 22 ])
[ { count: { lowerBound: Long('1') }, facet: { birthdateFacet: { buckets: [ { _id: ISODate('1970-01-01T00:00:00.000Z'), count: Long('1') }, { _id: ISODate('1980-01-01T00:00:00.000Z'), count: Long('0') }, { _id: ISODate('1990-01-01T00:00:00.000Z'), count: Long('0') } ] } } } ]