Request.findOne({_id: id}).populate("user").exec(function(err, request) {
if (!err) {
request.user.firstname = firstname;
request.date = date;
request.save(next);
}
After save, date has changed but the user's firstname has not.
Model :
var Request = new Schema({
user: {
type: ObjectId,
ref: 'User',
required: true
},
date: {
type: Date,
default: Date.now()
},
I can still do it by doing
request.user.save(function(err){
if(!err)
request.save(next)
});
But why doesn't the first one work ?
request and user are documents in separate collections so they each require their own save operation. That's simply the way Mongoose works (and MongoDB in general) as no update operation affects more than one collection at a time.
Check the 'Updating Refs' section of the Mongoose documentation on populate.
You will see there, in the example, that you still must save both the Request and User documents you have created. (In the example, they first save the user 'guille' then in the callback they save the 'story'.)
In Mongoose, SubDocs save when a parent doc saves but .populate does not do this.