![](/img/trans.png)
[英]$aggregation and $look up in the same collection- mongodb not get child
[英]$aggregation and $look up in the same collection- mongodb
結構或多或少像;
[
{id: 1, name: "alex" , children: [2, 4, 5]},
{id: 2, name: "felix", children: []},
{id: 3, name: "kelly", children: []},
{id: 4, name: "hannah", children: []},
{id: 5, name: "sonny", children: [6]},
{id: 6, name: "vincenzo", children: []}
]
當children
數組不為空時,我想用名字替換children
ID。
所以查詢的結果是預期的;
[ {id: 1, name: "alex" , children: ["felix", "hannah" , "sonny"]}
{id: 5, name: "sonny", children: ["vincenzo"]}
]
我做了什么來實現這一目標;
db.list.aggregate([
{$lookup: { from: "list", localField: "id", foreignField: "children", as: "children" }},
{$project: {"_id" : 0, "name" : 1, "children.name" : 1}},
])
用它的父母填充孩子,這不是我想要的:)
{ "name" : "alex", "parent" : [ ] }
{ "name" : "felix", "parent" : [ { "name" : "alex" } ] }
{ "name" : "kelly", "parent" : [ ] }
{ "name" : "hannah", "parent" : [ { "name" : "alex" } ] }
{ "name" : "sonny", "parent" : [ { "name" : "alex" } ] }
{ "name" : "vincenzo", "parent" : [ { "name" : "sonny" } ] }
我誤解了什么?
在使用$lookup
階段之前,你應該使用$unwind
for children數組然后$lookup
for children。 在$lookup
階段之后,你需要使用$group
來獲取帶有name而不是id的 children數組
你可以試試:
db.list.aggregate([
{$unwind:"$children"},
{$lookup: {
from: "list",
localField: "children",
foreignField: "id",
as: "childrenInfo"
}
},
{$group:{
_id:"$_id",
children:{$addToSet:{$arrayElemAt:["$childrenInfo.name",0]}},
name:{$first:"$name"}
}
}
]);
// can use $push instead of $addToSet if name can be duplicate
為何使用$group
?
例如:您的第一份文件
{id: 1, name: "alex" , children: [2, 4, 5]}
在$unwind
你的文件會是這樣的
{id: 1, name: "alex" , children: 2},
{id: 1, name: "alex" , children: 4},
{id: 1, name: "alex" , children: 5}
在$lookup
{id: 1, name: "alex" , children: 2,
"childrenInfo" : [
{
"id" : 2,
"name" : "felix",
"children" : []
}
]},
//....
然后在$group
{id: 1, name: "alex" , children: ["felix", "hannah" , "sonny"]}
使用當前的Mongo 3.4版本,您可以使用$graphLookup
。
對於非遞歸查找, $maxDepth
設置為0
。 您可能希望在查找之前添加$match
階段以過濾沒有子項的記錄。
db.list.aggregate([{
$graphLookup: {
from: "list",
startWith: "$children",
connectFromField: "children",
connectToField: "id",
as: "childrens",
maxDepth: 0,
}
}, {
$project: {
"_id": 0,
"name": 1,
"childrenNames": "$childrens.name"
}
}]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.