我有一个这样的 mongo 数据库:
db.templates.insertMany( [
{ _id: 1, uuid: "1", name: "t1", related_templates: [ "2", "2" ] },
{ _id: 2, uuid: "2", name: "t2", related_templates: [ "3", "3" ] },
{ _id: 3, uuid: "3", name: "t3", related_templates: [ "4", "4" ] },
{ _id: 4, uuid: "4", name: "t4"},
] )
如您所见,数据表示树结构,但支持对同一子节点的重复引用。我正在尝试从 t1 开始递归地获取整个树,包括重复的引用。
结果将如下所示:
{
"_id" : 1,
"uuid" : "1",
"name": "t1",
"related_templates" : [
{
"_id" : 2,
"uuid" : "2",
"name" : "t2",
"related_templates" : [
{
"_id" : 3,
"uuid" : "3",
"name" : "t3",
"related_templates" : [
{
"_id" : 4,
"uuid" : "4",
"name" : "t4"
},
{
"_id" : 4,
"uuid" : "4",
"name" : "t4"
}
]
},
{
"_id" : 3,
"uuid" : "3",
"name" : "t3",
"related_templates" : [
{
"_id" : 4,
"uuid" : "4",
"name" : "t4"
},
{
"_id" : 4,
"uuid" : "4",
"name" : "t4"
}
]
}
]
},
...(t2 repeats here)
]
}
Mongo 网站上建议的解决方案在这里:https ://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#std-label-unwind-example 。如果没有重复引用,则此解决方案效果很好,只需稍作修改,甚至还允许递归查找。但是,在我的情况下,我需要保留重复的查找
我也考虑过使用 unwind + group 的传统解决方案。该解决方案确实保留了重复项,但我还没有弄清楚如何递归地使用它。
我还考虑过使用 mongo 网站上的解决方案来获取不重复的内容,然后使用地图将获取的数据附加到原始的 related_templates 数组中。我认为这会起作用,但它似乎不是很优雅。
是否有一个优雅/更简单的解决方案来做到这一点,我错过了?