抱歉,我的英语不好...
如何使用mongoose查找子匹配?
我的查询是
categorys.aggregate([{
"$sort": {
"order": 1,
"id": 1
}
},
{
"$lookup": {
"localField": "id",
"from": "categorys",
"foreignField": "parentId",
"as": "child"
}
},
{
"$match": {
see: true,
depth: 1
}
}
]).exec(function(err, Categorys)
结果如下
{
"_id": "596e237c414f2137b0c4e9c2",
"id": 3,
"name": "DB",
"address": "DB",
"parentId": 0,
"depth": 1,
"see": true,
"__v": 0,
"child": [{
"_id": "596e24701e1bd30dc415b894",
"id": 5,
"name": "Mongodb",
"address": "Mongodb",
"parentId": 3,
"depth": 2,
"see": true,
"__v": 0
},
{
"_id": "596e24821e1bd30dc415b895",
"id": 6,
"name": "mssql",
"address": "mssql",
"parentId": 3,
"depth": 2,
"see": false,
"__v": 0
}
]
}]
我在 MSSQL 中没有找到结果(see:false),这是怎么回事?请帮忙!
$lookup
,但这可能不是最好的选择。实际上,你似乎在询问类似于Aggregation filter after $lookup的问题,尽管标题如此,但实际上它本身就是关于“自连接”的。而不同的方法在答案中有所解释。 - Neil Lunn