How would you populate the product fields inside the cart array below?
{
"_id": "5bbd1c6e2c48f512fcd733b4",
"cart": [
{
"count": 1,
"_id": "5bbd30ed9417363f345b15fc",
"product": "5ba93a6d5d907d9512e43b75"
}
{
"count": 2,
"_id": "5bbd30ed9417363f345babcc",
"product": "5ba93a6d5d2359512e43b75"
}
],
"totalItems": 0,
"name": "kellie"
}
Schema is:
const usersSchema = new mongoose.Schema({
cart: [
{
product: {type: mongoose.Schema.Types.ObjectId, ref: 'product'},
count: {type: Number},
},
],
totalItems: {type: Number, default: 0},
totalPrice: {type: Number},
});
I tried to research a solution with no luck.
I reproduced you schema (the main part with the cart.product
) and had no issues populating via the usual populate.exec()
method:
var result = Author.findOne({ _id: "5bbd93a29fceda195bec8665" })
.populate("books.book")
.exec()
.then(result => {
console.log(result)
})
My Author schema had same card.product
thing but it is books.book
:
var AuthorSchema = new Schema({
books: [{
book: { type: Schema.Types.ObjectId, ref: "Book" },
count: {type: Number}
}
]
}
Same happens with Author.find({})
etc.
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.