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

in Operator

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>]
}
}
}
字段
类型
说明
必要性

path

字符串或字符串数组

要搜索的一个或多个带索引字段。您还可以指定通配符路径进行搜索。

要搜索字段中的字符串值,您必须将该字段索引为MongoDB搜索词元类型。

必需

score

对象

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

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

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

  • function:使用函数表达式替换结果分数。

Optional

value

要搜索的一个或多个值。值可以是单个值或仅包含一种支持的 BSON 类型的值数组,而不能是不同类型的混合。

要搜索字段中的字符串值,您必须将该字段索引为MongoDB搜索词元类型。

必需

doesNotAffect

字符串或字符串数组

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

Optional

The following examples use the in operator to query collections in the sample_analytics.customers collection. If you load the sample data on your cluster and create a MongoDB Search index named default that uses static mappings on the collection, you can run the following queries against the collections.

示例索引定义指定了以下操作,以支持针对集合中索引字段的 in 操作员查询:

  • 自动索引集合中所有可动态索引的字段。

  • name 字段静态索引为词元类型,并将字段中的文本转换为小写。

{
"mappings": {
"index": "default",
"dynamic": true,
"fields": {
"name": {
"normalizer": "lowercase",
"type": "token"
}
}
}
}

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

The following query uses the in operator to search the birthdate field, which contains a single value, for customers who were born on given dates. The query uses the $project stage to:

  • 省略结果中的_id字段。

  • 在结果中仅包含 namebirthdate 字段。

1db.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 会返回与查询中指定的日期相匹配的两个文档。

The following query uses the in operator to query the accounts field, which contains an array of numbers, for customers with account numbers 371138, 371139, or 371140. The query uses the $project stage to:

  • 省略结果中的_id字段。

  • 在结果中仅包含 nameaccounts 字段。

1db.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 匹配的文档。

The following query uses the text operator to query for customers whose first name is James in the name field. The query specifies preference using the in operator for customers associated with the given objectIds in the _id field. The query uses $limit stage to limit the output to 5 results and the $project stage to:

  • 在结果中仅包含 _idname 字段。

  • 将名为score的字段添加到结果中。

1db.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 SanchezJennifer 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 分段的排除性上界

1db.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')
}
]
}
}
}
]