MongoDB 聚合数组和嵌套查找
MongoDB aggregate with array and nested lookup
我正在努力将一个查询放在一起,以完全按照我的需要聚合数据。
如果有人可以提供帮助,我将不胜感激!
collections是:
Collection 1
[
{
"_id": 1,
"name": "Collection 1:1",
"collection_2_ids": [5, 6]
},
{
"_id": 2,
"name": "Collection 1:2",
"collection_2_ids": [8, 9]
}
]
Collection 2
[
{
"_id": 5,
"name": "collection 2:5",
"collection_1_id": 1,
"collection_3_id": 12
},
{
"_id": 6,
"name": "collection 2:6",
"collection_1_id": 1,
"collection_3_id": 13
},
{
"_id": 8,
"name": "collection 2:8",
"collection_1_id": 2,
"collection_3_id": 14
},
{
"_id": 9,
"name": "collection 2:9",
"collection_1_id": 2,
"collection_3_id": 15
},
]
Collection 3:
[
{
"_id": 12,
"name": "collection 3:12"
},
{
"_id": 13,
"name": "collection 3:13"
},
{
"_id": 14,
"name": "collection 3:14"
},
{
"_id": 15,
"name": "collection 3:15"
}
]
我想要的是:
[
{
"_id": 1,
"name": "Collection 1:1",
"collection_2_documents": [
{
"_id": 5,
"name": "collection 2:5",
"collection_1_id": 1,
"collection_3_id": 12,
"collection_3_document": {
"_id": 12,
"name": "collection 3:12"
}
},
{
"_id": 6,
"name": "collection 2:6",
"collection_1_id": 1,
"collection_3_id": 13,
"collection_3_document": {
"_id": 12,
"name": "collection 3:12"
}
}
]
},
{
"_id": 2,
"name": "Collection 1:2",
"collection_2_documents": [
{
"_id": 8,
"name": "collection 2:8",
"collection_1_id": 2,
"collection_3_id": 14,
"collection_3_document": {
"_id": 14,
"name": "collection 3:14"
}
},
{
"_id": 9,
"name": "collection 2:9",
"collection_1_id": 2,
"collection_3_id": 15,
"collection_3_document": {
"_id": 15,
"name": "collection 3:15"
}
}
]
}
]
这是我当前的 aggregate/lookup,returns 将文档数组与 collections 2 和 3 分开。
[
{
$lookup: {
from: 'Collection 2',
localField: 'collection_2_ids',
foreignField: '_id',
as: 'collection_2_documents'
}
},
{
$lookup: {
from: 'Collection 3',
localField: 'collection_2_documents.collection_3_id',
foreignField: '_id',
as: 'collection_3_document'
}
}
]
您必须使用 $lookup
阶段的 pipeline
选项,并在 collection2 的查找
中的 collection3 上使用嵌套 $lookup
db.collection1.aggregate([
{
"$lookup": {
"from": "collection2",
"let": {
"sourceCollection_2_ids": "$collection_2_ids"
},
"pipeline": [
{
"$match": {
"$expr": {
"$in": [
"$_id",
"$$sourceCollection_2_ids"
]
}
}
},
{
"$lookup": {
"from": "collection3",
"let": {
"sourceCollection_3_id": "$collection_3_id"
},
"pipeline": [
{
"$match": {
"$expr": {
"$eq": [
"$_id",
"$$sourceCollection_3_id"
]
},
}
},
],
"as": "collection_3_document"
}
},
{
"$set": {
"collection_3_document": {
"$arrayElemAt": [
"$collection_3_document",
0
]
}
}
},
],
"as": "collection_2_documents"
}
},
])
我正在努力将一个查询放在一起,以完全按照我的需要聚合数据。 如果有人可以提供帮助,我将不胜感激!
collections是:
Collection 1
[
{
"_id": 1,
"name": "Collection 1:1",
"collection_2_ids": [5, 6]
},
{
"_id": 2,
"name": "Collection 1:2",
"collection_2_ids": [8, 9]
}
]
Collection 2
[
{
"_id": 5,
"name": "collection 2:5",
"collection_1_id": 1,
"collection_3_id": 12
},
{
"_id": 6,
"name": "collection 2:6",
"collection_1_id": 1,
"collection_3_id": 13
},
{
"_id": 8,
"name": "collection 2:8",
"collection_1_id": 2,
"collection_3_id": 14
},
{
"_id": 9,
"name": "collection 2:9",
"collection_1_id": 2,
"collection_3_id": 15
},
]
Collection 3:
[
{
"_id": 12,
"name": "collection 3:12"
},
{
"_id": 13,
"name": "collection 3:13"
},
{
"_id": 14,
"name": "collection 3:14"
},
{
"_id": 15,
"name": "collection 3:15"
}
]
我想要的是:
[
{
"_id": 1,
"name": "Collection 1:1",
"collection_2_documents": [
{
"_id": 5,
"name": "collection 2:5",
"collection_1_id": 1,
"collection_3_id": 12,
"collection_3_document": {
"_id": 12,
"name": "collection 3:12"
}
},
{
"_id": 6,
"name": "collection 2:6",
"collection_1_id": 1,
"collection_3_id": 13,
"collection_3_document": {
"_id": 12,
"name": "collection 3:12"
}
}
]
},
{
"_id": 2,
"name": "Collection 1:2",
"collection_2_documents": [
{
"_id": 8,
"name": "collection 2:8",
"collection_1_id": 2,
"collection_3_id": 14,
"collection_3_document": {
"_id": 14,
"name": "collection 3:14"
}
},
{
"_id": 9,
"name": "collection 2:9",
"collection_1_id": 2,
"collection_3_id": 15,
"collection_3_document": {
"_id": 15,
"name": "collection 3:15"
}
}
]
}
]
这是我当前的 aggregate/lookup,returns 将文档数组与 collections 2 和 3 分开。
[
{
$lookup: {
from: 'Collection 2',
localField: 'collection_2_ids',
foreignField: '_id',
as: 'collection_2_documents'
}
},
{
$lookup: {
from: 'Collection 3',
localField: 'collection_2_documents.collection_3_id',
foreignField: '_id',
as: 'collection_3_document'
}
}
]
您必须使用 $lookup
阶段的 pipeline
选项,并在 collection2 的查找
$lookup
db.collection1.aggregate([
{
"$lookup": {
"from": "collection2",
"let": {
"sourceCollection_2_ids": "$collection_2_ids"
},
"pipeline": [
{
"$match": {
"$expr": {
"$in": [
"$_id",
"$$sourceCollection_2_ids"
]
}
}
},
{
"$lookup": {
"from": "collection3",
"let": {
"sourceCollection_3_id": "$collection_3_id"
},
"pipeline": [
{
"$match": {
"$expr": {
"$eq": [
"$_id",
"$$sourceCollection_3_id"
]
},
}
},
],
"as": "collection_3_document"
}
},
{
"$set": {
"collection_3_document": {
"$arrayElemAt": [
"$collection_3_document",
0
]
}
}
},
],
"as": "collection_2_documents"
}
},
])