MongoDB将数组转换为树结构

时间:2015-11-18 12:11:39

标签: mongodb aggregation-framework

我有存储在MongoDB集合中的文档 - 文档使用数组映射到层次结构中的位置:

db.docs.find({})

{
    _id : 564a22329a8cbd78022738f6,
    path: ['root','people','janice','orders'],
    name: "janice-order-1",
    value: 36
},
{
    _id : 564a22329a8cbd78022738f7,
    path: ['root','people','george','sales'],
    nameL "george-sale-1",
    value:19
}

这相当于树结构:

root
  people
    janice
      orders
        janice-order-1 : 36
    george
      sales
        george-sale-1  : 19

我希望能够查询给定的treeposition(例如['root','people'])和给定的depth,例如2,树的哪些分支存在:

> branches(['root','people'], 2)
{
    "janice":{
       "orders":null
    },
    "george":{
       "sales":null
    }
}

> branches([], 4)
 {"root":{
    "people":{
      "janice":{
        "orders":null
       }
      "george":{
        "sales": null
      }
    }
  }

我可以用MongoDB以优雅的方式做到这一点吗?

0 个答案:

没有答案
相关问题