node.js - 架构 : Mongoose modelling 中的递归元素

标签 node.js mongodb mongoose schema

知道如何在 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": {}
});

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

最佳答案

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

By design, it might work. There's no tests for this. I have Schema#add in there for this purpose, to produce recursive references:

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

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

关于node.js - 架构 : Mongoose modelling 中的递归元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33825773/

相关文章:

mysql - 如何返回查询函数内的值?

javascript - bookshelf js 中的多个 order By() 列

node.js - 如何在服务器端的node.js中设计每天执行的常规作业?

node.js - 使用 mongoose 和 mongoose-version 更新条目时的新版本

node.js - 如何在node.js中验证 Mongoose 连接mongodb

javascript - CoffeeScript + Express.js : cannot call method 'sliced' of undefined

node.js - node/express REST API 应用程序 - 将参数传递给 GET 方法

mongodb - 文档数据库中的链接与引用

node.js - Mongodb 数据模型策略指导/建议

mongodb - Mongodb 中不允许为 null