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

embeddedDocument Operator

embeddedDocument

The embeddedDocument operator is similar to $elemMatch operator. It constrains multiple query predicates to be satisfied from a single element of an array of embedded documents. embeddedDocument can be used only for queries over fields of the embeddedDocuments type.

embeddedDocument 通过以下语法实现:

{
"embeddedDocument": {
"path": "<path-to-field>",
"operator": { <operator-specification> },
"score": { <score-options> }
}
}

embeddedDocument 使用以下选项构建查询:

字段
类型
说明
必要性

operator

对象

用于查询您在 path 中指定的文档数组中的每个文档的操作符。不支持 moreLikeThis 操作符。

必需

path

字符串

要搜索的索引 embeddedDocuments 类型字段。指定的字段必须是使用 operator 选项指定的所有操作符和字段的父字段。

必需

score

对象

分配给匹配搜索结果的分数。您可以使用 embedded 评分选项配置评分选项。要了解更多信息,请参阅评分行为。

Optional

您无法突出显示 embeddedDocument 操作符中的查询。

注意

MongoDB Search 停止在副本集或单个分片上复制大于每个分区 2、100、000、000索引对象的索引更改,其中每个带索引的嵌入式父文档都算作单个对象。超过此限制可能会导致查询结果过时。

Using the embeddedDocuments field type can result in indexing objects over this index size limit, because each indexed embedded document is counted as a single object. If you create a MongoDB Search index that has or will soon have more than 2.1 billion index objects, use the numPartitions index option to partition your index (supported only on Search Nodes deployments) or shard your cluster.

当您使用 embeddedDocument操作符查询数组中的嵌入式文档时, MongoDB Search 会在查询执行的不同阶段对查询操作符查询谓词进行评估和评分。MongoDB搜索:

  1. 独立评估数组中的每个嵌入式文档。

  2. 合并使用 embedded 选项配置的匹配结果的分数;如果未指定 embedded 分数选项,则汇总以合并匹配结果的分数。

  3. 如果通过 compound 指定了其他查询谓词,则将匹配结果与父文档连接在一起。

默认情况下,embeddedDocument 操作符使用默认聚合策略 (sum) 合并嵌入式文档匹配的分数。embeddedDocument 操作符 score 选项允许您覆盖默认值,并使用 embedded 选项配置匹配结果的分数。

要按嵌入式文档字段对父文档进行排序,必须执行以下操作:

  • 将嵌入式文档子字段的父项索引为文档类型。

  • 将嵌入文档中带有 string 值的子字段索引为标记类型。对于带有数字和日期值的子字段,启用动态映射可自动为这些字段编制索引。

MongoDB Search 仅对父文档进行排序。它不会对文档大量中的子字段进行排序。有关示例,请参阅排序示例。

对于在 embeddedDocument 操作符中指定的查询谓词,如果字段根据 document 类型的父字段进行索引,您可以突出显示这些字段。有关示例,请参阅教程

To learn about the embeddedDocument operator limitations, see embeddedDocument Operator Limitations.

The following examples use the sample_supplies.sales collection in the sample dataset.

这些示例查询对集合使用以下索引定义:

{
"mappings": {
"dynamic": true,
"fields": {
"items": [
{
"dynamic": true,
"type": "embeddedDocuments"
},
{
"dynamic": true,
"fields": {
"tags": {
"type": "token"
}
},
"type": "document"
}
],
"purchaseMethod": {
"type": "token"
}
}
}
}

The following query searches the collection for items tagged school with a preference for items named backpack. MongoDB Search scores the results in descending order based on the average (arithmetic mean) score of all matching embedded documents. The query includes a $limit stage to limit the output to 5 documents and a $project stage to:

  • 排除 items.nameitems.tags 字段以外的所有字段

  • 添加字段 score

1db.sales.aggregate({
2 "$search": {
3 "embeddedDocument": {
4 "path": "items",
5 "operator": {
6 "compound": {
7 "must": [{
8 "text": {
9 "path": "items.tags",
10 "query": "school"
11 }
12 }],
13 "should": [{
14 "text": {
15 "path": "items.name",
16 "query": "backpack"
17 }
18 }]
19 }
20 },
21 "score": {
22 "embedded": {
23 "aggregate": "mean"
24 }
25 }
26 }
27 }
28},
29{
30 $limit: 5
31},
32{
33 $project: {
34 "_id": 0,
35 "items.name": 1,
36 "items.tags": 1,
37 "score": { $meta: "searchScore" }
38 }
39})
[
{
items: [ {
name: 'backpack',
tags: [ 'school', 'travel', 'kids' ]
} ],
score: 1.2907354831695557
},
{
items: [ {
name: 'envelopes',
tags: [ 'stationary', 'office', 'general' ]
},
{
name: 'printer paper',
tags: [ 'office', 'stationary' ]
},
{
name: 'backpack',
tags: [ 'school', 'travel', 'kids' ]
} ],
score: 1.2907354831695557
},
{
items: [ {
name: 'backpack',
tags: [ 'school', 'travel', 'kids' ]
} ],
score: 1.2907354831695557
},
{
items: [ {
name: 'backpack',
tags: [ 'school', 'travel', 'kids' ]
} ],
score: 1.2907354831695557
},
{
items: [ {
name: 'backpack',
tags: [ 'school', 'travel', 'kids' ]
} ],
score: 1.2907354831695557
}
]

以下查询搜索标记为 school 且优先搜索名为 backpack 的项目。它请求有关 purchaseMethod 字段的分面信息。

1db.sales.aggregate({
2 "$searchMeta": {
3 "facet": {
4 "operator": {
5 "embeddedDocument": {
6 "path": "items",
7 "operator": {
8 "compound": {
9 "must": [
10 {
11 "text": {
12 "path": "items.tags",
13 "query": "school"
14 }
15 }
16 ],
17 "should": [
18 {
19 "text": {
20 "path": "items.name",
21 "query": "backpack"
22 }
23 }
24 ]
25 }
26 }
27 }
28 },
29 "facets": {
30 "purchaseMethodFacet": {
31 "type": "string",
32 "path": "purchaseMethod"
33 }
34 }
35 }
36 }
37})
[
{
count: { lowerBound: Long("2309") },
facet: {
purchaseMethodFacet: {
buckets: [
{ _id: 'In store', count: Long("2751") },
{ _id: 'Online', count: Long("1535") },
{ _id: 'Phone', count: Long("578") }
]
}
}
}
]

The following query searches for items named laptop and it sorts the results by the items.tags field. The query includes a $limit stage to limit the output to 5 documents and a $project stage to:

  • 排除 items.nameitems.tags 之外的所有字段

  • 添加字段 score

1db.sales.aggregate({
2 "$search": {
3 "embeddedDocument": {
4 "path": "items",
5 "operator": {
6 "text": {
7 "path": "items.name",
8 "query": "laptop"
9 }
10 }
11 },
12 "sort": {
13 "items.tags": 1
14 }
15 }
16},
17{
18 "$limit": 5
19},
20{
21 "$project": {
22 "_id": 0,
23 "items.name": 1,
24 "items.tags": 1,
25 "score": { "$meta": "searchScore" }
26 }
27})
1[
2 {
3 items: [
4 { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ] },
5 { name: 'binder', tags: [ 'school', 'general', 'organization' ] },
6 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] },
7 { name: 'laptop', tags: [ 'electronics', 'school', 'office' ] },
8 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] },
9 { name: 'printer paper', tags: [ 'office', 'stationary' ] },
10 { name: 'backpack', tags: [ 'school', 'travel', 'kids' ] },
11 { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ] },
12 { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ] }
13 ],
14 score: 1.168686032295227
15 },
16 {
17 items: [
18 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] },
19 { name: 'binder', tags: [ 'school', 'general', 'organization' ] },
20 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] },
21 { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ] },
22 { name: 'printer paper', tags: [ 'office', 'stationary' ] },
23 { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ] },
24 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] },
25 { name: 'backpack', tags: [ 'school', 'travel', 'kids' ] },
26 { name: 'laptop', tags: [ 'electronics', 'school', 'office' ] }
27 ],
28 score: 1.168686032295227
29 },
30 {
31 items: [
32 { name: 'backpack', tags: [ 'school', 'travel', 'kids' ] },
33 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] },
34 { name: 'binder', tags: [ 'school', 'general', 'organization' ] },
35 { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ] },
36 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] },
37 { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ] },
38 { name: 'laptop', tags: [ 'electronics', 'school', 'office' ] }
39 ],
40 score: 1.168686032295227
41 },
42 {
43 items: [
44 { name: 'laptop', tags: [ 'electronics', 'school', 'office' ] },
45 { name: 'binder', tags: [ 'school', 'general', 'organization' ] },
46 { name: 'binder', tags: [ 'school', 'general', 'organization' ] },
47 { name: 'backpack', tags: [ 'school', 'travel', 'kids' ] },
48 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] },
49 { name: 'printer paper', tags: [ 'office', 'stationary' ] },
50 { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ] },
51 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] },
52 { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ] },
53 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] }
54 ],
55 score: 1.168686032295227
56 },
57 {
58 items: [
59 { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ] },
60 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] },
61 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] },
62 { name: 'backpack', tags: [ 'school', 'travel', 'kids' ] },
63 { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ] },
64 { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ] },
65 { name: 'binder', tags: [ 'school', 'general', 'organization' ] },
66 { name: 'laptop', tags: [ 'electronics', 'school', 'office' ] },
67 { name: 'printer paper', tags: [ 'office', 'stationary' ] },
68 { name: 'binder', tags: [ 'school', 'general', 'organization' ] }
69 ],
70 score: 1.168686032295227
71 }
72]

The following query returns only the nested documents that match the query. The query uses MongoDB Search compound operator clauses in the $search stage to find matching documents and then the aggregation operators in the $project stage to return only matching embedded documents. Specifically, the query specifies the following pipeline stages:

复合操作符 must 子句中指定以下条件:

  • 检查集合中是否存在 items.price 字段。

  • items.tags 字段中搜索标记为 school 的项目。

  • 仅当 items.quantity 字段的值大于 2 时才匹配。

将输出限制为 5 份文档。

请执行以下操作:

  • 排除 _id 字段,仅包含 itemsstoreLocation 字段。

  • Use $filter to return only elements of the items input array that match the condition specified using the $and operator. The and operator uses the following operators:

    • $ifNull to determine if items.price contains null values and replace null values, if present, with the replacement expression false.

    • $gt 检查数量是否大于 2。

    • $in to check if office exists in the tags array.

1db.sales.aggregate(
2 {
3 "$search": {
4 "embeddedDocument": {
5 "path": "items",
6 "operator": {
7 "compound": {
8 "must": [
9 {
10 "range": {
11 "path": "items.quantity",
12 "gt": 2
13 }
14 },
15 {
16 "exists": {
17 "path": "items.price"
18 }
19 },
20 {
21 "text": {
22 "path": "items.tags",
23 "query": "school"
24 }
25 }
26 ]
27 }
28 }
29 }
30 }
31 },
32 {
33 "$limit": 2
34 },
35 {
36 "$project": {
37 "_id": 0,
38 "storeLocation": 1,
39 "items": {
40 "$filter": {
41 "input": "$items",
42 "cond": {
43 "$and": [
44 {
45 "$ifNull": [
46 "$$this.price", "false"
47 ]
48 },
49 {
50 "$gt": [
51 "$$this.quantity", 2
52 ]
53 },
54 {
55 "$in": [
56 "office", "$$this.tags"
57 ]
58 }
59 ]
60 }
61 }
62 }
63 }
64 }
65)
1[
2 {
3 storeLocation: 'Austin',
4 items: [
5 {
6 name: 'laptop',
7 tags: [ 'electronics', 'school', 'office' ],
8 price: Decimal128('753.04'),
9 quantity: 3
10 },
11 {
12 name: 'pens',
13 tags: [ 'writing', 'office', 'school', 'stationary' ],
14 price: Decimal128('19.09'),
15 quantity: 4
16 },
17 {
18 name: 'notepad',
19 tags: [ 'office', 'writing', 'school' ],
20 price: Decimal128('30.23'),
21 quantity: 5
22 },
23 {
24 name: 'pens',
25 tags: [ 'writing', 'office', 'school', 'stationary' ],
26 price: Decimal128('20.05'),
27 quantity: 4
28 },
29 {
30 name: 'notepad',
31 tags: [ 'office', 'writing', 'school' ],
32 price: Decimal128('22.08'),
33 quantity: 3
34 },
35 {
36 name: 'notepad',
37 tags: [ 'office', 'writing', 'school' ],
38 price: Decimal128('21.67'),
39 quantity: 4
40 }
41 ]
42 },
43 {
44 storeLocation: 'Austin',
45 items: [
46 {
47 name: 'notepad',
48 tags: [ 'office', 'writing', 'school' ],
49 price: Decimal128('24.16'),
50 quantity: 5
51 },
52 {
53 name: 'notepad',
54 tags: [ 'office', 'writing', 'school' ],
55 price: Decimal128('28.04'),
56 quantity: 5
57 },
58 {
59 name: 'notepad',
60 tags: [ 'office', 'writing', 'school' ],
61 price: Decimal128('21.42'),
62 quantity: 5
63 },
64 {
65 name: 'laptop',
66 tags: [ 'electronics', 'school', 'office' ],
67 price: Decimal128('1540.63'),
68 quantity: 3
69 },
70 {
71 name: 'pens',
72 tags: [ 'writing', 'office', 'school', 'stationary' ],
73 price: Decimal128('29.43'),
74 quantity: 5
75 },
76 {
77 name: 'pens',
78 tags: [ 'writing', 'office', 'school', 'stationary' ],
79 price: Decimal128('28.48'),
80 quantity: 5
81 }
82 ]
83 }
84]

要学习;了解更多信息,请参阅如何对嵌入式文档中的字段运行MongoDB搜索查询。