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

in Operator

MongoDB Search 中的 in 操作符在给定路径中搜索 BSON number、date、boolean、objectId、uuid 或 string 值的数组,并返回字段值等于指定数组中的任何值的文档。如果字段包含一个数组,则 in 操作符选择字段包含数组的文档,并且该数组至少包含一个与指定数组中的任何值匹配的元素。

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字段。

  • 在结果中仅包含 name 和 birthdate 字段。

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

以下查询使用 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')
}
]
}
}
}
]