Skip to content Skip to sidebar Skip to footer

Sub-Schemas On Mongoose Without Arrays

So, I was wondering, even though I understood that you cannot create a single sub-document, I still would like to create a sub-document so that I can use default and other mongoose

Solution 1:

The schema of embedded objects need to be defined using plain objects, so if you want to keep the definitions separate you can do it as:

var SomeOther = {
    a              : { type:String, default:'test' },
    b              : { type:Boolean, default:false }
    ...
};
var SomeOtherSchema = new Schema(SomeOther); // Optional, if needed elsewhere

var GroupSettings = {
    x              : { type:Number, default:20 },
    y              : { type:Boolean, default:false },
    ...
    else           : SomeOther
};
var GroupSettingSchema = new Schema(GroupSettings); // Optional, if needed elsewhere

var GroupSchema = new Schema({
    name                : { type:String , required:true, unique:true},
    description         : { type:String, required:true },
    ...
    settings            : GroupSettings
});

Post a Comment for "Sub-Schemas On Mongoose Without Arrays"