Debe crear un nuevo objeto al declarar su CollectPoint
incrustado elementos:
var data = new CollectPoint({
name: "Level 1",
collectPoints: [
new CollectPoint({
name: "Level 1.1",
collectPoints: []
})
]
});
De esta forma el _id
y collectPoints
será creado por la instanciación de CollectPoint
de lo contrario, solo está creando un JSONObject simple.
Para evitar ese tipo de problemas, cree un validador para su matriz que generará un error si sus elementos tienen un tipo incorrecto:
var CollectPointSchema = new mongoose.Schema({
name: { type: String },
collectPoints: {
type: [this],
validate: {
validator: function(v) {
if (!Array.isArray(v)) return false
for (var i = 0; i < v.length; i++) {
if (!(v[i] instanceof CollectPoint)) {
return false;
}
}
return true;
},
message: 'bad collect point format'
}
}
});
De esta manera, lo siguiente generará un error:
var data = new CollectPoint({
name: "Level 1",
collectPoints: [{
name: "Level 1.1",
collectPoints: []
}]
});