如何在Mongoose的帮助下创建MongoDB数据集的分层树结构

时间:2017-03-09 08:03:09

标签: node.js mongodb angular mongoose mongodb-query

我有一个具有4级分层结构的分层数据集,如下图所示。我已将数据导入MongoDB,如下面示例摘录中所示。

我的目标是在我在此处找到的树组件中显示此数据:https://github.com/500tech/angular-tree-component

这个lib想要JSON中的数据,如下所示:

[
  {
    id: 1,
    name: 'root1',
    isExpanded: true,
    children: [
      {
        id: 2,
        name: 'child1'
      }, {
        id: 3,
        name: 'child2'
      }
    ]
  }
]

“name”应该连接到“preferredName”和“id”连接到“codedName”,但我应该如何将父节点与子节点相关联,我应该在哪里进行,已经在MongoDB中或者在一个对象之后Javascript(数据集有大约50.000个唯一的8位数代码)?我所拥有的是8位数ID结构,您可以看到示例加上连接到每个唯一8位数代码的4个级别: 13000000段 13010000主要集团 13010100集团 13010101商品类

http://wiki.eclass.eu/w/images/1/12/EClass_tree_structure_example_01.JPG

MongoDB示例:

{
    "_id" : ObjectId("58c106f886102c16f0489a01"),
    "mkKeyword" : "0",
    "mkSubclass" : "1",
    "level" : "1",
    "definition" : "Services for the development of a product basically on the basis of service contracts or contract development",
    "preferredName" : "Development (Service)",
    "codedName" : "13000000",
    "__v" : 0
}

/* 3 */
{
    "_id" : ObjectId("58c106f886102c16f0489a02"),
    "mkKeyword" : "0",
    "mkSubclass" : "1",
    "level" : "2",
    "definition" : "Total or partial service for product developments",
    "preferredName" : "Concept development",
    "codedName" : "13010000",
    "__v" : 0
}

/* 4 */
{
    "_id" : ObjectId("58c106f886102c16f0489a03"),
    "mkKeyword" : "0",
    "mkSubclass" : "1",
    "level" : "3",
    "definition" : "Total or partial service for product developments",
    "preferredName" : "Feasibility analysis",
    "codedName" : "13010100",
    "__v" : 0
}

/* 5 */
{
    "_id" : ObjectId("58c106f886102c16f0489a04"),
    "mkKeyword" : "0",
    "mkSubclass" : "0",
    "level" : "4",
    "definition" : "Sub-group (4th level) for objects that cannot be classified into other specified sub-groups in the existing structure, but that are classified to their parent-class on the 3rd level",
    "preferredName" : "Feasibility analysis (unspecified)",
    "codedName" : "13010190",
    "__v" : 0
}

Mongoose Schema:

const EclassSchema = new mongoose.Schema({
        codedName: { type: String, min: 10, max: 10 },
        preferredName: { type: String, max: 80 },
        definition: { type: String, max: 1023 },
        level: { type: String, min: 1, max: 1 },
        mkSubclass: { type: String, min: 1, max: 1 },
        mkKeyword: { type: String, min: 1, max: 1 }
    });

0 个答案:

没有答案