我不知道如何选择 '$graphLookup 或 $lookup' 其他类似的
期待mongodb官方更完整的文档
例子:
{parentId: 0, cid: 1, other: 'a'},
{parentId: 0, cid: 2, other: 'b'},
{parentId: 0, cid: 3, other: 'c'},
{parentId: 1, cid: 11, other: 'aa'},
{parentId: 2, cid: 12, other: 'ab'},
{parentId: 3, cid: 13, other: 'ac'},
结果:
{
parentId: 0, cid: 1, other: 'a',
children: [
{parentId: 1, cid: 11, other: 'aa'},
]
},{
parentId: 0, cid: 2, other: 'b',
children: [
{parentId: 2, cid: 12, other: 'ab'},
]
},{
parentId: 0, cid: 3, other: 'c',
children: [
{parentId: 3, cid: 13, other: 'ac'},
]
}
},
怎么办?
你需要使用$graphLookup
db.collection.aggregate([
{
$match: {
"parentId": {
$eq: 0
}
}
},
{
$graphLookup: {
from: "collection",
startWith: "$cid",
connectFromField: "cid",
connectToField: "parentId",
as: "children"
}
}
])
$lookup用于“加入”集合进行处理。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句