【发布时间】:2014-12-29 15:27:10
【问题描述】:
我正在尝试返回与特定用户 ID 相关联的优惠券列表并填充 _merchant 引用。
这个查询正确地填充了 _merchant 引用。如果我可以补充:
其中的 coupon.users.contains(myuserid) 到我的查询中会得到我需要的东西
db.couponModel.find().populate('_merchant').exec(function(err, coupons) {
res.send(coupons);
});
或者这个查询找到了我需要的正确优惠券。如果我可以补充:
populate(_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' }],
});
我需要混合这两个查询来获得我想要的结果。
【问题讨论】:
-
我猜不支持在嵌入文档的嵌入文档上填充。见github.com/LearnBoost/mongoose/issues/601#issuecomment-6520568
-
我认为它在几天前的版本中得到了支持,但还没有文档。有什么技巧可以绕过它吗?也许使用 $in?