如何从mongo db递归获取数据?

时间:2018-07-17 21:58:35

标签: javascript mongodb

我在mongodb中有一些数据(我们称之为日志),像这样说:

{
  name: String,
  category_id: String
}

每个类别都有 parent_id 。我想要的是尽可能多地树到第一个父级,并从所谓的日志中获取我所获得的每一项数据的所有父级。

我首先想到的是:在控制器中获取所有项目,然后递归获取所有父项。它可能会起作用,但似乎很繁琐和错误。

在模型本身上可能有更好的方法,例如静态方法。

所以,我的问题是,您将如何使用mongodb做到这一点?我知道有聚合,我使用了几次,但是我可以看到如果通过具有特定值的特定字段使用聚合。但是在这里,您得到一个项目,通过parent_id等获得下一个项目,依此类推。

1 个答案:

答案 0 :(得分:1)

您必须查看$graphLookup聚合阶段。提供一组相关数据以获取更多帮助。

编辑:这里是一个示例:

---数据---

#logs collection
db.logs.find({});
{ 
    "_id" : ObjectId("5b4f2970d42ef3178d108e86"), 
    "name" : "01", 
    "category" : "cat1"
}
{ 
    "_id" : ObjectId("5b4f2981d42ef3178d108e87"), 
    "name" : "02", 
    "category" : "cat1"
}
{ 
    "_id" : ObjectId("5b4f298ad42ef3178d108e88"), 
    "name" : "03", 
    "category" : "cat2"
}
{ 
    "_id" : ObjectId("5b4f2997d42ef3178d108e89"), 
    "name" : "04", 
    "category" : "cat2"
}
{ 
    "_id" : ObjectId("5b4f29bed42ef3178d108e8a"), 
    "name" : "015", 
    "category" : "cat10"
}

#categories collection
db.categories.find({});
{ 
    "_id" : "cat1", 
    "parent_id" : "cat2"
}
{ 
    "_id" : "cat2", 
    "parent_id" : "cat10"
}
{ 
    "_id" : "cat10"
}

---聚集查询---

db.logs.aggregate(
    [
        {
            $graphLookup: {
                from: "categories",
                startWith: "$category", // connectToField value(s) that recursive search starts with
                connectFromField: "parent_id",
                connectToField: "_id",
                as: "related_categories",
                maxDepth: 10, // optional
                depthField: "depthField" // optional - name of field in output documents
            }
        },
    ],
);

---输出---

{ 
    "_id" : ObjectId("5b4f2970d42ef3178d108e86"), 
    "name" : "01", 
    "category" : "cat1", 
    "related_categories" : [
        {
            "_id" : "cat10", 
            "depthField" : NumberLong(2)
        }, 
        {
            "_id" : "cat2", 
            "parent_id" : "cat10", 
            "depthField" : NumberLong(1)
        }, 
        {
            "_id" : "cat1", 
            "parent_id" : "cat2", 
            "depthField" : NumberLong(0)
        }
    ]
}
{ 
    "_id" : ObjectId("5b4f2981d42ef3178d108e87"), 
    "name" : "02", 
    "category" : "cat1", 
    "related_categories" : [
        {
            "_id" : "cat10", 
            "depthField" : NumberLong(2)
        }, 
        {
            "_id" : "cat2", 
            "parent_id" : "cat10", 
            "depthField" : NumberLong(1)
        }, 
        {
            "_id" : "cat1", 
            "parent_id" : "cat2", 
            "depthField" : NumberLong(0)
        }
    ]
}
{ 
    "_id" : ObjectId("5b4f298ad42ef3178d108e88"), 
    "name" : "03", 
    "category" : "cat2", 
    "related_categories" : [
        {
            "_id" : "cat10", 
            "depthField" : NumberLong(1)
        }, 
        {
            "_id" : "cat2", 
            "parent_id" : "cat10", 
            "depthField" : NumberLong(0)
        }
    ]
}
{ 
    "_id" : ObjectId("5b4f2997d42ef3178d108e89"), 
    "name" : "04", 
    "category" : "cat2", 
    "related_categories" : [
        {
            "_id" : "cat10", 
            "depthField" : NumberLong(1)
        }, 
        {
            "_id" : "cat2", 
            "parent_id" : "cat10", 
            "depthField" : NumberLong(0)
        }
    ]
}
{ 
    "_id" : ObjectId("5b4f29bed42ef3178d108e8a"), 
    "name" : "015", 
    "category" : "cat10", 
    "related_categories" : [
        {
            "_id" : "cat10", 
            "depthField" : NumberLong(0)
        }
    ]
}