[英]Mongoose populate nested element inside of multiple arrays
我有以下架構:
const userSchema = new Schema({
...,
categories: [
{
name: {
type: String,
required: true
},
products: [
{
type: mongoose.Types.ObjectId,
required: false,
ref: 'Product'
}
]
}
],
...
}
我想獲得用戶擁有的所有產品。
我已經看到更多關於這個主題的問題,但我沒有讓它發揮作用。
如果您輸入的數據正確,您可以這樣做:
let result = await User.findById(id).populate("categories.products").lean()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.