Mongoose (v3.8) + Node.js
I have a model called 'Products' which has this field:
upvoted_by: [{
user_id: { type: Schema.ObjectId, ref: 'Users' },
timestamp: { type: Date }
}]
And the Users
model has a bunch of other fields.
To add an upvote, I do this:
product.upvoted_by.push({
user_id: req.user,
timestamp: new Date()
});
I'm trying to populate the upvoted_by.user_id
field so that it contains the corresponding user data.
I tried this but it doesn't seem to work:
// 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));
});
Any idea what's going wrong and how to fix it?