【问题标题】:Recursive elements in Schema : Mongoose modellingSchema 中的递归元素:Mongoose 建模
【发布时间】:2016-02-22 21:17:30
【问题描述】:

知道如何在 Mongoose Schema 中为 Tree 文档建模吗?

var TreeSchema = new Schema({
    "Non-leafNode": {
        "children": [
            {
                "type": "NodeElement"
            }
        ]
    },
    "NodeElement": {
        // one of them is required. not both.
        "elem": {
            "type": "LeafNode"
        },
        "elem2": {
            "type": "Non-leafNode"
        }
    },
    "LeafNode": {}
});

一个人怎么能做到这一点?整个树是一个文档(理想情况下)。

【问题讨论】:

    标签: node.js mongodb mongoose schema


    【解决方案1】:

    来自https://groups.google.com/forum/#!topic/mongoose-orm/0yUVXNyprx8

    按照设计,它可能会起作用。对此没有任何测试。为此,我在其中有Schema#add,以生成递归引用:

    var Tasks = new Schema();
    Tasks.add({
       title     : String
     , subtasks  : [Tasks]
    });
    

    所以你需要一步一步地构造递归。

    【讨论】:

    • 你将如何填充它?如果您想获取所有任务的所有后代的所有信息
    • 这一切都在一个 Mongo 文档中,所以它已经被填充了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 2019-10-22
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 2022-06-28
    相关资源
    最近更新 更多