I am working with the following schema in Mongoose:
// Note that Child is a first-class schema and not used only for sub-documents!
var ChildSchema = new Schema({
name: String
});
mongoose.model('Child', ChildSchema);
var ParentSchema = new Schema({
name: String,
children: [{
childId: {
type: ObjectId,
ref: 'Child',
required: true
}
}]
});
mongoose.model('Parent', ParentSchema);
When I push one or more children into the parent document, like below. Note that these are code snippets, so please disregard that the snippets look like these are executing synchronously, although the order of execution is the same.
// snippet# 1
var child = new Child({ name: 'Connor' });
child.save();
// snippet# 2
var parent = new Parent({name: 'Bob'});
parent.save();
// snippet# 3
parent.locations.push({ childId: child.id });
parent.save();
The resulting document looks like this:
{
"name" : "Bob",
"children" : [ {
"childId" : ObjectId("57769de13eaeda6020b522f2"),
"_id" : ObjectId("5776a23a511539a421824c27")
} ]
}
I expected to see only the childId field in the parent document, as childId holds the ID value corresponding to the previously created child. The _id field is inserted automatically. Is there any way to avoid the _id field and have childId only?