Prevent updatedAt change in Mongoose findOneAndUpdate

Starting with mongoose 5.9.3, you can set the third parameter to { timestamps: false }.

From the docs:

[options.timestamps=null] «Boolean» If set to false and schema-level timestamps are enabled, skip timestamps for this update. Note that this allows you to overwrite timestamps. Does nothing if schema-level timestamps are not set.


After scouring the Mongoose documentation, it appears that the short answer is no, there is no way to call findOneAndUpdate() without the updatedAt field being set. If you view the docs here, it clearly states that all of the functions for updating (findOneAndUpdate(), update() and bulkWrite()) add the $set operator to the query to set updatedAt. That being said, you do have another option which I would highly recommend.

Work Around

Add the createdAt and updatedAt fields directly to your models, and get rid of the timestamps. Sure, it's really nice that Mongoose handles it for you, but in the end, if you have to try and change the set functionality of the timestamps for a specific use case, it would be better to do it on your own.

If you enjoy having the updatedAt and createdAt set when you call .save() or .update(), you can always write your own middleware to add in the operations.

ProfileSchema.pre('save', function(next) {
  this.createdAt = Date.now();
  this.updatedAt = Date.now();
  next();
});

I understand this isn't the desired solution, but quite frankly I find it more useful since have greater control, and less unexpected behavior from Mongoose. The more control you have over your own code, the better. Hope this helps!