Mongoose populate with array of objects containing ref

前端 未结 4 590
旧巷少年郎
旧巷少年郎 2020-12-07 10:39

I have a Mongoose schema with an array lists of objects that consist of a reference to another collection and a nested array of numbers:

var Sch         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-07 11:12

      // Cart schema  
    
     var CartSchema = new mongooseSchema({
            productDetails: [
                {
                    productId: {
                        type: mongoose.Schema.ObjectId,
                        required: true,
                        ref:'Product'
    
                    },
                    productCount: Number,
                }
            ],
            UserId: {
                type: String,
                default: '',
                required: true,
                trim: true,
            },
            shopId: {
                type: String,
                default: '',
                required: true,
                trim: true,
            },
        });
    
      // add this .populate('productDetails.productId').
    
             db.Cart.find({
                            UserId: userId,
                            shopId: shopId
                        }).populate('productDetails.productId').skip(pagination.skip).limit(pagination.limit).exec(function (error, CartList) {
    
                            if (error) {
                                callback(error, null)
                            } else {
                                callback(null, CartList)
                            }
                        });
    

提交回复
热议问题