[英]How would you populate references nested in an object of an array using mongoose?
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: 我重现了您的架构(主要部分是
cart.product
),并且没有通过常规populate.exec()
方法populate.exec()
问题:
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
: 我的作者架构具有相同的
card.product
东西,但它是books.book
:
var AuthorSchema = new Schema({
books: [{
book: { type: Schema.Types.ObjectId, ref: "Book" },
count: {type: Number}
}
]
}
Same happens with Author.find({})
etc. Author.find({})
等Author.find({})
发生同样的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.