我有一個items收藏:
[
{_id: '1', name: 'A', description: 'Some description 1'},
{_id: '2', name: 'B', description: 'Some description 2'},
{_id: '3', name: 'C', description: 'Some description 3'},
{_id: '4', name: 'D', description: 'Some description 4'},
]
我還有另一個收藏cItems:
[
{_id: '1', itemId: '1', items:[ {itemId: '2', qty: 4}, {itemId: '3', qty: 5} ]},
{_id: '2', itemId: '4', items:[ {itemId: '2', qty: 1}, {itemId: '3', qty: 1} ]},
]
我正在嘗試使用聚合獲取基于集合itemId的專案的詳細資訊。CItems
cItems.aggregate([
{ $match: { _id: { $eq: categoryId } } },
{ $unwind: "$items" },
{
$lookup: {
from: "items",
let: {
itemId: "$items.itemId",
items: "$items",
},
pipeline: [
{ $match: { $expr: { $eq: ["$_id", "$$itemId"] } } },
{
$replaceRoot: {
newRoot: { $mergeObjects: ["$$items", "$$ROOT"] },
},
},
{
$project: {
qty: 1,
name: 1,
description: 1,
},
},
],
as: "items",
},
},
{
$group: {
_id: "$_id",
items: { $push: { $first: "$items" } },
name: { $first: "$name" },
description: { $first: "$description" },
},
},
]);
我得到的結果是:
{
"_id": "1",
"items": [
{
"qty": 4,
"_id": "2",
"name": "A",
"description": "Some description 2"
},
{
"qty": 5,
"_id": "3",
"name": "C",
"description": "Some description 3"
}
],
"name": null,
"description": null
}
問題是它為不在陣列中的 itemId 的名稱和描述顯示空值。
預期輸出:
{
"_id": "1",
"items": [
{
"qty": 4,
"_id": "2",
"name": "A",
"description": "Some description 2"
},
{
"qty": 5,
"_id": "3",
"name": "C",
"description": "Some description 3"
}
],
"name": 'A',
"description": "Some description 1"
}
我正在使用最新版本的 MongoDB。
uj5u.com熱心網友回復:
您錯過了與父專案 ( ) [第 2 階段]$lookup一起加入的階段。itemsitemId
而要獲取父項的名稱和描述,您需要$arrayElemAt獲取parentItem[最后階段]的第一項。
解決方案 1
db.cItems.aggregate([
{
$match: {
_id: {
$eq: categoryId
}
}
},
{
$lookup: {
from: "items",
localField: "itemId",
foreignField: "_id",
as: "parentItem"
}
},
{
"$unwind": "$items"
},
{
$lookup: {
from: "items",
let: {
itemId: "$items.itemId",
items: "$items"
},
pipeline: [
{
$match: {
$expr: {
$eq: [
"$_id",
"$$itemId"
]
}
}
},
{
$replaceRoot: {
newRoot: {
$mergeObjects: [
"$$items",
"$$ROOT"
]
}
}
},
{
$project: {
qty: 1,
name: 1,
description: 1
}
}
],
as: "items"
}
},
{
$group: {
_id: "$_id",
items: {
$push: {
$first: "$items"
}
},
name: {
$first: {
"$arrayElemAt": [
"$parentItem.name",
0
]
}
},
description: {
$first: {
"$arrayElemAt": [
"$parentItem.description",
0
]
}
}
}
}
])
示例 Mongo Playground(解決方案 1)
解決方案 2
db.cItems.aggregate([
{
$match: {
_id: {
$eq: categoryId
}
}
},
{
$lookup: {
from: "items",
localField: "itemId",
foreignField: "_id",
as: "parentItem"
}
},
{
$lookup: {
from: "items",
let: {
items: "$items"
},
pipeline: [
{
$match: {
$expr: {
$in: [
"$_id",
"$$items.itemId"
]
}
}
},
{
"$replaceRoot": {
"newRoot": {
"$mergeObjects": [
{
$arrayElemAt: [
"$$items",
0
]
},
"$$ROOT"
]
}
}
}
],
as: "items"
}
},
{
$project: {
_id: 1,
itemId: 1,
items: 1,
name: {
$arrayElemAt: [
"$parentItem.name",
0
]
},
description: {
$arrayElemAt: [
"$parentItem.description",
0
]
}
}
}
])
示例 Mongo Playground(解決方案 2)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/450655.html
