Hello! Faced with the problem
I have long-live object of User model, wich I can save several times during its life. Each user have array of Region model, which i populate when querying User.
But after that i need somehow to populate for user new regions. And I can't. Second user.populate returns same array of doc's as first time. What can I do?
var User = new mongoose.Schema({
login: {type: String, required: true, index: { unique: true }},
regions: [{type: Schema.Types.ObjectId, ref: 'Region'}]
});
var Region = new Schema({
cid: {type: Number, index: {unique: true}}
});
user.populate({path: 'regions', select: {_id: 0, cid: 1, title_en: 1, title_local: 1}}, function (err, user) {
console.log(user.regions); // <-- [{cid: 1}, {cid: 2}]
user.update({$set: {regions: some_new_ids }}, function (err, numberAffected, raw) {
user.populate({path: 'regions', select: {_id: 0, cid: 1, title_en: 1, title_local: 1}}, function (err, user) {
console.log(user.regions); // <-- AGAIN [{cid: 1}, {cid: 2}]
});
});
});