findByIdAndUpdate does not have a 'pre' hook

1,372 views
Skip to first unread message

Billy Newman

unread,
Feb 17, 2016, 4:02:27 PM2/17/16
to Mongoose Node.JS ODM
I am trying to set a pre hook on findByIdAndUpdate, however it does not seem that hook exists.

The good news is that I added findOneAndUpdate pre hook and that pre hook seems to get called when I make a call to findByIdAndUpdate.

However when I grab the query object in the pre hook it does not have the id:

ModelSchema.pre('findOneAndUpdate', function(next) {
 
var query = this.getQuery();

...


});


Model.findByIdAndUpdate(id, {something: 'foo'}, function(err, model) {
...
}



In my hook the query object is:

{something: 'foo'}

But I cannot tell what id the original query was looking for.


Billy Newman

unread,
Feb 17, 2016, 4:10:57 PM2/17/16
to Mongoose Node.JS ODM
And...... nevermind, I was running 4.2.7.  When on 4.2.7 the post is correct.

However after upgrading to 4.4.4 I can do this and it works properly:

ModelSchema.pre('findOneAndUpdate', function(next) {
 
var query = this.getQuery();  // contains id
 
var update = this.getUpdate();

...


});
Reply all
Reply to author
Forward
0 new messages