How to populate field of an embedded document?
I have an OrderSchema, which contains "Invoice" as an embedded schema. I want to populate a field ("series") from the nested schema.
The schema looks like the following:
const OrderSchema = new Schema({
success: {
type: Boolean,
},
invoice: {
type: new Schema({
series: {
// NEEDS TO POPULATE
type: Schema.Types.ObjectId,
ref: "Series",
required: true,
},
number: {
type: Number,
required: true,
},
}, {
_id: false,
timestamps: false
}),
required: true,
},
});
Here, I need to populate the path "invoice.series". How can I achieve this?
You can populate it like this
OrderModel.find(query)
.populate({
path: 'invoice',
populate: {
path: 'series',
}
})
.exec(function(err, docs) {});
or optionally you can do this ...
OrderModel.find(query)
.populate("invoice.series")
.exec(function(err, docs) {});