创建命名分支作为子文档

时间:2013-12-29 22:02:38

标签: node.js mongodb

用户文档将具有分支,这些分支具有名称,并且还将用作分支。示例文档可能类似于:

{
    name: "username",
    // trunk
    branches: {
        foo: {
            branches: {
                bar: {
                    branches: {}
                }
            }
    }
}

我可以通过类似

之类的内容向bar添加新分支
db.user.update({name: "foo"}, {"$set":
    {"branches.foo.branches.bar.branches.baz": {branches: {}}
})

分支机构还会提供其他信息,因为它在这里并不重要。

我的问题是以这种方式存储子文档更好,还是更好地拥有一个额外的属性(名称)并搜索它?例如:

{
    name: "username",
    branches: [{
        name: "foo",
        branches: []
    }]
}

在后一种情况下,我实际上不确定如何搜索子文档,因为我没有尝试过它。

是否存在将命名子文档存储为属性与数组的偏好?

0 个答案:

没有答案