Reputation: 59
I have a queried document, which I populated from other documents. My question is whether it is possible to update the referenced document or do I have to create new query for the other document using the id fields.
Example (code in mongoosejs):
Users.findById(id).populate('library.id', null, 'BookModel').exec(function(err, user)
user.library.id.loc.coordinates = [40, 20];
user.save();
});
Upvotes: 0
Views: 79
Reputation: 311835
As stated in the docs for populate
:
The documents returned from query population become fully functional, removeable, saveable documents unless the lean option is specified. Do not confuse them with sub docs. Take caution when calling its remove method because you'll be removing it from the database, not just the array.
So yes, you can modify the referenced, populated documents directly and call save
on them to commit any changes. But you need to call save
on the referenced document to do it:
Users.findById(id).populate('library.id', null, 'BookModel').exec(function(err, user)
user.library.id.loc.coordinates = [40, 20];
user.library.id.save();
});
Upvotes: 1