Can I use mongoose SubDocuments recursively? I have this object:
var Player = {
city: {
energy: {
solar: 20,
fusion: 0
}
}
};
and corresponding schemas:
var PlayerSchema = new Schema({
city: CitySchema
});
PlayerSchema.pre('save', function(next){
this.city = {};
});
var EnergySchema = new mongoose.Schema({
solar: {type: Number, default: 0},
fusion: {type: Number, default: 0}
});
var CitySchema = new mongoose.Schema({
last_update: { type: Date, default: Date.now },
energy: EnergySchema
});
CitySchema.pre('save', function (next) {
this.energy = {};
});
But saving this object saves only city without energy. (During PlayerSchema.pre('save', ...)
with command this.city = {};
is created Object from CitySchema with default values, but without taking in note method CitySchema.pre('save', ...)
which leads to undefined energy attribute in city.)
I would like to avoid populating and making references through ObjectId.
Is possible to save Player object with just subdocuments?