Tush Bedva
Tush Bedva

Reputation: 67

How can I have infinitely nested Schema in Mongoose?

i want to create mongoose nested schema for infinite nesting

like :

   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]
     });

where : title is root subtitle children subsubtitiles children....n goes on

Please Help ME!!

Upvotes: 0

Views: 305

Answers (1)

HoefMeistert
HoefMeistert

Reputation: 1200

You just need to reference your schema, like :

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},

   }); 

This way you can add a workstructure(s) in subTitles, this workstructure can contain another workstructure etc..etc..etc....

Hope it's clear.

Upvotes: 1

Related Questions