如何编写查找多个路径的MongoDB查询



这是我使用的集合:

- sign1: A1
- sign2: A2
- sign1: A2
- sign2: A5
- sign1: A2
- sign2: A6
- sign1: A2
- sign2: A8
- sign1: A5
- sign2: A8

查询应该找到从A1到A8 的路径

例如,它应该找到:

path1:
A1 A2 -> A2 A5 -> A5 A8
path2:
A1 A2 -> A2 A8
path3:
A1 A2 -> A2 A6 -> should be ignored since not finishes with A8

目前,我尝试了这个(@ray的部分解决方案):

我的查询

我的查询的第一个问题是,即使没有以A8 结束,它也会返回所有路径

第二个问题是,不分离路径会将所有内容都放在一个阵列中

要过滤并仅返回以A8结尾的路径,必须在$graphLookup之后再次使用$match阶段。

您可以尝试这样的操作,只返回以A8结尾的路径

db.collection.aggregate([
{
"$match": {
sign1: "A1"
}
},
{
"$graphLookup": {
"from": "collection",
"startWith": "$sign1",
"connectFromField": "sign2",
"connectToField": "sign1",
"as": "path",
"depthField": "step"
}
},
{
"$match": {
"path.sign2": "A8"
}
}
])

现在,如果你想将路径分隔成不同的数组/文档,你必须在$match阶段之前使用$unfold阶段,如下所示:

db.collection.aggregate([
{
"$match": {
sign1: "A1"
}
},
{
"$graphLookup": {
"from": "collection",
"startWith": "$sign1",
"connectFromField": "sign2",
"connectToField": "sign1",
"as": "path",
"depthField": "step"
}
},
{
"$unwind": "$path"
},
{
"$match": {
"path.sign2": "A8"
}
}
])

编辑时间:如果要筛选不需要的路径或step等于0的路径,可以像这样在$graphLookup阶段中使用$filter操作,以返回基于这两个条件的结果。

db.collection.aggregate([
{
"$match": {
sign1: "A1"
}
},
{
"$graphLookup": {
"from": "collection",
"startWith": "$sign1",
"connectFromField": "sign2",
"connectToField": "sign1",
"as": "path",
"depthField": "step",
"maxDepth": 10,
"filter": {
"$and": [
{ "step": { "$gt": 0 } },
{ "sign2": "A8" }
]
}
}
},
{
"$unwind": "$path"
}
])

相关内容

  • 没有找到相关文章

最新更新