I needed to have child documents in side paranet document. Expecting child document in a seperate collection.
var childSchema = new Schema({ name: 'string' });
var parentSchema = new Schema({
children: [childSchema]
})
var Parent = mongoose.model('Parent', parentSchema);
var parent = new Parent({ children: [{ name: 'Matt' }, { name: 'Sarah' }] })
parent.save(callback);
Here children document does not stored in the separate collections called 'childschemas'. It is always stored inside Parent collection itself. But i wanted to search child schema collections.
Am i miss anything?
You have specified children to be stored in the document, just using a schema for validation. If you want subdocuments to be stored in a separate model you'd specify it as such:
children: [{ type: mongoose.Schema.ObjectId, ref: 'Model' }]
Note that Model is the name of a model, not a schema.