How do I populate an index inside of a list? Say I have the following schema's
const supplierSchema = new mongoose.Schema({
name: String,
address: String,
size: Number,
... (19 other fields)
supplier_id: mongoose.Schema.Types.ObjectId,
});
const itemsSchema = new mongoose.Schema({
name: String,
date_time: {type: Date, default: Date.now},
supplier_id: mongoose.Schema.Types.ObjectId,
item_id: mongoose.Schema.Types.ObjectId,
});
const receiptSchema = new mongoose.Schema({
date_time: {type: Date, default: Date.now},
items: [itemSchema]
receipt_id: mongoose.Schema.Types.ObjectId,
});
const Receipt = mongoose.model('Photo', photoSchema);
My Receipt is populated such that
Receipt.findById(<insertID>) currently gives:
{
receipt_id: ObjectId("..."),
date_time: ISODate("..."),
items: [
{
name: "...",
date_time: ISODate("..."),
supplier_id: ObjectId("..."),
item_id: ObjectId("...")
},
{
name: "...",
date_time: ISODate("..."),
supplier_id: ObjectId("..."),
item_id: ObjectId("...")
}
],
receipt_id: ObjectId("..."),
}
I would like for it to populate the supplier ID
with name
and address
.
You can populate across multiple levels using populate
:
Receipt
.findById(<insertID>)
.populate({
path: 'items',
populate: { path: 'supplier_id' }
})
.exec()
For more information see the official docs
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.