2013-02-13 156 views
5

我试图返回与特定用户标识关联的优惠券列表并填充_merchant引用。猫鼬嵌套填充查询

此查询正确填充_merchant参考。如果我可以添加:

其中coupon.users.contains(myuserid)到我的查询会得到什么,我需要

db.couponModel.find().populate('_merchant').exec(function(err, coupons) { 
    res.send(coupons); 
}); 

或者此查询发现,我需要正确的优惠券。如果我可以添加:

填充(_merchant)到我的查询,也将得到我所需要的。

db.userModel.findById(req.params.id).populate('coupons').exec(function(err, user) { 
    res.send(user.coupons) 
}); 

架构

var userSchema = new Schema({ 
    email: { type: String, required: true , unique: true }, 
    coupons: [{ type: Schema.ObjectId, ref: 'Coupon' }] 
}); 

var couponSchema = new Schema({ 
    _merchant: { type: Schema.ObjectId, ref: 'Merchant', required: true }, 
    couponid: { type: Number, required: true, unique: true }, 
    users: [{ type: Schema.ObjectId, ref: 'User' }] 


}); 

var merchantSchema = new Schema({ 
    name: { type: String, required: true , unique: true } 
    coupons: [{ type: ObjectId, ref: 'Coupon' }], 

}); 

我需要这两个查询的一些混合得到我想要的东西。

+0

我猜想嵌入式文档的嵌入式文档尚不支持。请参阅https://github.com/LearnBoost/mongoose/issues/601#issuecomment-6520568 – idursun 2013-02-13 12:59:26

+0

我认为它在几天前的发行版中受到支持,但目前还没有文档。任何黑客来解决它?使用$也许? – user1071182 2013-02-13 13:26:55

+0

https://github.com/LearnBoost/mongoose/pull/1292 – user1071182 2013-02-13 13:39:37

回答

0

使用$所有选项。 ('users':{$ all:[req.params.id]}})。populate('_ merchant')。exec(function(err,coupons){console.log(coupons);})