猫鼬 (v3.8) + Node.js
我有一个名为“产品”的模型,其中包含以下字段:
upvoted_by: [{
user_id: { type: Schema.ObjectId, ref: 'Users' },
timestamp: { type: Date }
}]
该Users
模型还有许多其他领域。
要添加赞成票,我这样做:
product.upvoted_by.push({
user_id: req.user,
timestamp: new Date()
});
我正在尝试填充该upvoted_by.user_id
字段以使其包含相应的用户数据。
我试过这个,但它似乎不起作用:
// product (= a document) has been found before
product.populate('upvoted_by.user_id', {
select: 'username' // username is a field in the Users model
}, function(err, doc) {
console.log(JSON.stringify(doc));
});
知道出了什么问题以及如何解决吗?