首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MongoDB $graphLookup获取所有级别的深嵌套结果。

MongoDB $graphLookup获取所有级别的深嵌套结果。
EN

Stack Overflow用户
提问于 2018-06-08 20:24:24
回答 2查看 8.8K关注 0票数 4

https://www.slideshare.net/mongodb/webinar-working-with-graph-data-in-mongodb中所示,幻灯片50可以在视图上使用$graphLookup,以获得嵌套格式中的2层深树结构。

我有一个以树节点作为文档的MongoDB集合,其格式如下:

代码语言:javascript
复制
{ "_id" : { "$oid" : "5b1a952361c6fa3418a15660" }, 
"nodeId" : 23978995, 
"name" : "settings", 
"type" : "Node",
"parentId" : [ 23978893, 23979072, 23979081 ] }

我创建了一个视图,如:

代码语言:javascript
复制
db.createView("treeView", "node", [
{
 $graphLookup: {
    from: "node",
    startWith: "$nodeId",
    connectFromField: "nodeId",
    connectToField: "parentId",
    maxDepth: 0,
    as: "children"
 }
}
]);

我执行图形查找,例如:

代码语言:javascript
复制
db.node.aggregate([ 
{ $match: {"nodeId": 23978786 } },
{
 $graphLookup: {
    from: "treeView",
    startWith: "$nodeId",
    connectFromField: "nodeId",
    connectToField: "parentId",
    maxDepth: 0,
    as: "children"
 }
}
]);

我的问题是,我怎样才能让整个层次,所有层次都很深?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-12 09:11:54

不幸的是,您无法获得嵌套格式的全部深度。使用视图是一种解决方法,它允许您执行该操作,但您需要为所需的每个嵌入级别创建一个新视图。相反,在在应用程序级别计算树之前,我会考虑在不提供深度的情况下执行graphLookup,从根级别开始,在一个查询中获取所有层次结构。

看起来就像这样:

代码语言:javascript
复制
db.node.aggregate([
    { $match: {
        parentId: null
    }},
    { $graphLookup: {
        from: "node",
        startWith: "$nodeId",
        connectFromField: "nodeId",
        connectToField: "parentId",
        depthField: "depth",
        as: "children"
    }}
]);

这应该允许您一次获取整个层次结构,因此接下来,您需要根据“子”数组中的信息计算应用程序中的树。

票数 14
EN

Stack Overflow用户

发布于 2019-03-08 07:07:39

@kmandala,我从最近两天开始就面临着这样的问题,我的收藏有一点不同,但概念和你的一样--我希望我写的东西能帮助你得到结果,(我参考了这样的答案)

“我的集合模式”类似于:

代码语言:javascript
复制
const Category = new Schema({
    sTitle: { type: String, trim: true },
    iParentId: { type: mongoose.Schema.Types.ObjectId, ref: 'Category' },
    bStatus: { type: Boolean, default: true },
    dUpdatedAt: { type: Date },
    dCreatedAt: { type: Date, default: Date.now }
});

首先,我使用$graphLookup,而不是把所有的孩子都分成一个合适的家长,比如:

代码语言:javascript
复制
{
  "_id": "5c6fa228c30bbf02cf12fe6c",
   "sTitle": "firstParent",
   "childrens":[{obj},{obj},{obj},{obj}]    // Childrens as well as grandChild
},
{
  "_id": "5c80d644ab57dd06d48cc474",
   "sTitle": "secondParent",
   "childrens":[]     //No Child
},
.....

在得到这种结果之后,我在节点js中创建了一棵树,而不使用任何第三方库。树逻辑代码是:(!注意:docs是$graphlooup输出的东西)

代码语言:javascript
复制
function list_to_tree(list) {
            var map = {}, node, roots = [], i;
            for (i = 0; i < list.length; i += 1) {
                map[list[i]._id] = i;
                list[i].children = [];
            }
            for (i = 0; i < list.length; i += 1) {
                node = list[i];
                if (node.iParentId !== null && map[node.iParentId] !== undefined) {
                    var node2 = {         //Because i need only _id,Title & childrens
                        _id: node._id,
                        Title: node.sTitle,
                        children: node.children
                    }
                    list[map[node.iParentId]].children.push(node2); //You can push direct "node"
                } else {
                    var node2 = {
                        _id: node._id,
                        Title: node.sTitle,
                        children: node.children
                    }
                    roots.push(node2);
                }
            }
            return roots;
        }
        let final_result = []     //For Storing all parent with childs
        if (docs.length >= 0) {   
            docs.map(single_doc => {  //For getting all parent Tree
                var single_child = list_to_tree(single_doc.children)
                var obj = {
                    _id: single_doc._id,
                    Title: single_doc.sTitle,
                    children: single_child
                }
                final_result.push(obj)
            })
        }
        console.log("Final Tree is : ",final_result)

我希望它能帮助你

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50767930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档