Schema中的递归元素:Mongoose建模

Uda*_*ddy 7 schema mongoose mongodb node.js

想知道在Mongoose Schema中建模Tree文档吗?

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

怎么可以这个模型?整个树是一个文件(理想情况下)

w00*_*00t 11

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

按照设计,它可能会起作用.没有对此进行测试.Schema#add为了这个目的,我在那里生成递归引用:

var Tasks = new Schema();
Tasks.add({
   title     : String
 , subtasks  : [Tasks]
});
Run Code Online (Sandbox Code Playgroud)

所以你需要逐步构造递归.

  • 所有内容都在一个 Mongo 文档中,因此已经填充 (2认同)