-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
updated timestamp not get updated for noop, aside of upsert usage #13170
Comments
As a workaround, you can explicitly set const user2 = await UsersModel.findOneAndUpdate({ name: 'test' }, { updated: Date.now() }, { new: true }) We're investigating this to see if this is a bug we can fix or expected behavior. |
I took a closer look and I think we're going to have to call this expected behavior. While we do always apply timestamps to The reasoning is that it is easier to apply timestamps to an empty update, than it is to tell Mongoose to not apply timestamps to empty updates if we make this change. To tell Mongoose to update const user2 = await UsersModel.findOneAndUpdate({ name: 'test' }, { updated: Date.now() }, { new: true }) Or add a plugin to handle that for all updates: mongoose.plugin(schema => {
schema.pre('findOneAndUpdate', function() {
if (this.getUpdate() == null || Object.keys(this.getUpdate()).length === 0) {
this.set('updatedAt', new Date());
}
});
}); Given that there's a simple workaround, we don't want to switch this behavior. |
fix(timestamps): set timestamps on empty `replaceOne()`
We found workaround for this one:
|
Prerequisites
Mongoose version
7.0.1
Node.js version
19.6.0
MongoDB server version
5.1.0
Typescript version (if applicable)
No response
Description
We requrie to update documents the way that only updated timestamp get changed, trying to perform update operation with empty object, which is noop. Mongoose seem to catching it and does not perform any operation. If we add upsert: true option though and document exists, mongoose does perform update even if it is noop. Well we can't use upsert as it may lead to many partial documents get inserted, but in the same time seems without upsert and without any actual update to the document we can't get updated timestmap to be updated.
Checked that in pure mongodb nodejs driver when we on update adding $set it is working as expected and update timestamp field every time
therefore it is issue on mongoose side.
Steps to Reproduce
Expected Behavior
updated timestamp get updated even on noop update operation, or there separate operations to only update timestmpas (e.g. bash touch) or there option to enforce update without creation if document not exists.
The text was updated successfully, but these errors were encountered: