我正在尝试运行一个$graphLookup
类似下面所示的示例:
目的是在给定特定记录($match在此处有注释)的情况下,通过immediateAncestors属性检索其完整的“路径” 。如您所见,这没有发生。
我$convert在这里介绍了_idfrom的处理方式,因为string相信可以与_idfrom immediateAncestors记录列表(是string)进行“匹配” 。
因此,我确实使用不同的数据运行了另一个测试(不ObjectId涉及):
db.nodos.insert({"id":5,"name":"cinco","children":[{"id":4}]})
db.nodos.insert({"id":4,"name":"quatro","ancestors":[{"id":5}],"children":[{"id":3}]})
db.nodos.insert({"id":6,"name":"seis","children":[{"id":3}]})
db.nodos.insert({"id":1,"name":"um","children":[{"id":2}]})
db.nodos.insert({"id":2,"name":"dois","ancestors":[{"id":1}],"children":[{"id":3}]})
db.nodos.insert({"id":3,"name":"três","ancestors":[{"id":2},{"id":4},{"id":6}]})
db.nodos.insert({"id":7,"name":"sete","children":[{"id":5}]})
和查询:
db.nodos.aggregate( [
{ $match: { "id": 3 } },
{ $graphLookup: {
from: "nodos",
startWith: "$ancestors.id",
connectFromField: "ancestors.id",
connectToField: "id",
as: "ANCESTORS_FROM_BEGINNING"
}
},
{ $project: {
"name": 1,
"id": 1,
"ANCESTORS_FROM_BEGINNING": "$ANCESTORS_FROM_BEGINNING.id"
}
}
] )
...输出了我期望的结果(这5条记录直接或间接与id3 条记录相关):
{
"_id" : ObjectId("5afe270fb4719112b613f1b4"),
"id" : 3.0,
"name" : "três",
"ANCESTORS_FROM_BEGINNING" : [
1.0,
4.0,
6.0,
5.0,
2.0
]
}
问题是:有没有一种方法可以实现我一开始提到的对象?
我正在运行Mongo 3.7.9(来自官方Docker)
提前致谢!
杨魅力
相关分类