从MongoDB在Node.Js中创建一个JSON树 [英] Create a JSON tree in Node.Js from MongoDB

查看:83
本文介绍了从MongoDB在Node.Js中创建一个JSON树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从MongoDB创建一个JSON树几天。我使用子参考模型结构,其中Books是root node。

我正在尝试实现这种形式的JSON树:

I am trying for a couple of days to create a JSON tree from my MongoDB. I use the child reference model structure, where "Books" is the root node.
I am trying to achieve a JSON tree of this form:

[{
    title: "Books",
    children: [{
        title: "Programming",
        children: [{
            title: "Databases",
            children: [{
                title: "MongoDb"
            }, {
                title: "Postgres"
            }]
        }, {
            title: "Languages"
        }]
    }, {
        title: "Item 2"
    }, {
        title: "Item 3"
    }, {
        title: "Item 4"
    }]
}]

但我是真的很难让它成功。第一级(Books,Item 2/3/4)只能有5个项目,但是其他子菜单可以有无穷大。

But I am really struggling to make it works. The first level (Books, Item 2/3/4) can only have 5 items, but other submenus can have an infinity.

我如何实现转换形式调用Mongo会给这个树结构的数据数组?
提前致谢

How can I achieve the transformation form the array of data that the call to Mongo will give to this tree structure ? Thanks in advance

推荐答案

让我们说你必须关注数据(已经从db加载):

Lets say that you have to following data (already loaded from db):

var data = [
  { _id: "MongoDB", children: [] },
  { _id: "Postgres", children: [] },
  { _id: "Databases", children: [ "MongoDB", "Postgres" ] },
  { _id: "Languages", children: [] },
  { _id: "Programming", children: [ "Databases", "Languages" ] },
  { _id: "Books", children: [ "Programming" ] }
];

由于 _id 是唯一的,所以首先步骤你将它转换为字典,其中键是id:

Since _id is unique, then in first step you convert it to dictionary, where keys are ids:

var dct = {};
for (var i = 0; i < data.length; i++) {
    var doc = data[i];
    dct[doc._id] = doc;
}

现在循环数据数组再一次并设置子项:

Now you loop through data array one more time and set children:

for (var i = 0; i < data.length; i++) {
    var doc = data[i];
    var children = doc.children;
    var ref_children = [];
    for (var j = 0; j < children.length; j++) {
        var child = dct[children[j]]; // <-- here's where you need the dictionary
        ref_children.push(child);
    }
    doc.children = ref_children;
}

瞧,你已经完成了:

JSON.stringify(data);

编辑

如果你只想要root(节点不是任何其他节点的子节点),那么首先你必须找到它们:

If you want only roots (nodes which are not children of any other node), then first you have to find them:

var get_parent = function(node, docs) {
    for (var i = 0; i < docs.length; i++) {
        var doc = docs[i];
        if (doc.children.indexOf(node) != -1) {
            return doc;
        }
    }
    return null;
};

var roots = [];
for (var i = 0; i < docs.length; i++) {
    var doc = data[i];
    if (get_parent(doc, docs) === null) {
        roots.push(doc);
    }
}
JSON.stringify(roots);

更有效的方法是在解除引用子项时存储父项(与编辑上面的代码比较):

More efficient way would be to store parents when dereferencing children (compare with the code above EDIT):

for (var i = 0; i < data.length; i++) {
    var doc = data[i];
    var children = doc.children;
    var ref_children = [];
    for (var j = 0; j < children.length; j++) {
        var child = dct[children[j]]; // <-- here's where you need the dictionary
        child.has_parent = true; // <-- has a parent
        ref_children.push(child);
    }
    doc.children = ref_children;
}

var roots = [];
for (var i = 0; i < data.length; i++) {
    var doc = data[i];
    if (!doc.has_parent) {
        roots.push(doc);
    }
}
JSON.stringify(roots);

这篇关于从MongoDB在Node.Js中创建一个JSON树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆