我想在用户订购商品时获得产品详细信息,所以我保存在Order模式中,在productId中传递了产品模式的引用 这是我的架构:
const Schema = mongoose.Schema
const OrderSchema=new Schema({
productId:[{_id:{type:Schema.Types.ObjectId,ref:'products'},quantity:{type:Number}}],
status:{type:Number,required:true},
deliverDate:{type:String},
totalAmount:{type:Number,default:0},
userId:{type:Schema.Types.ObjectId,ref:'userDetail'},
Address:{type:Object,required:true},
totalquantity:{type:Number,default:0},
name:{type:String},
method:{type:String,required:true},
txnId:{type:String},
pushToken:{type:String},
coordinates: {
type: [Number, Number],
index: '2d'
},
},{timestamps: true})
module.exports=mongoose.model('orders',OrderSchema)
我想获取一个包含所有产品详细信息的productId的订单:
Order.findById(id)
.then(order=>{
res.json({
orders:order
})
}).catch(err=>{console.log(err);next("Network error")})
如何填充productId?