简体   繁体   English

Mongoose 填充多个嵌套元素 arrays

[英]Mongoose populate nested element inside of multiple arrays

I have the following schema:我有以下架构:

const userSchema = new Schema({
    ...,
    categories: [
        {
            name: {
                type: String,
                required: true
            },
            products: [
                {
                    type: mongoose.Types.ObjectId,
                    required: false,
                    ref: 'Product'
                }
            ]
        }
    ],
...
}

I want to get all the products a user have.我想获得用户拥有的所有产品。

I have seen more questions about this topic but I don't get it to work.我已经看到更多关于这个主题的问题,但我没有让它发挥作用。

If you entered your data correctly you can do like this:如果您输入的数据正确,您可以这样做:

let result = await User.findById(id).populate("categories.products").lean()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM