我怎样才能在Mongoose中拥有无限嵌套的Schema?

时间:2017-01-13 04:57:20

标签: arrays node.js mongodb mongoose

我想为无限嵌套创建mongoose嵌套模式

喜欢:

   var workstructureSchema = new mongoose.Schema({

   title : String,
   created_at:{type:Date, default : Date.now},
   deleted_at:Date,
   subTitles:[structureSchema],
   projectId : {type:ObjectId,ref : "Project"},
   locationId : {type: ObjectId, ref: 'Location'},
   workSpaceId : {type: ObjectId, ref: 'WorkSpace'},
   editor : {type: ObjectId, ref: 'Employee'},
   isDelete : {type:Boolean, default : false},

   }); 

    var structureSchema = new mongoose.Schema({
     childrensTitle:String,
     grandChildrens: [String]
     });

其中:title是root               副标题儿童               sububtitiles children .... n继续

请帮助我!!

1 个答案:

答案 0 :(得分:1)

您只需要引用您的架构,例如:

var workstructureSchema = new mongoose.Schema({

   title : String,
   created_at:{type:Date, default : Date.now},
   deleted_at:Date,
   subTitles: [{ type: Schema.ObjectId, ref: 'workstructureSchema (dont know how you called it)' }],
   projectId : {type:ObjectId,ref : "Project"},
   locationId : {type: ObjectId, ref: 'Location'},
   workSpaceId : {type: ObjectId, ref: 'WorkSpace'},
   editor : {type: ObjectId, ref: 'Employee'},
   isDelete : {type:Boolean, default : false},

   }); 

这样你可以在subTitles中添加一个工程结构,这个工程结构可以包含另一个工程结构等......等等......

希望它清楚。