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
wrongly update timestamps on updateOne #14250
Comments
https://mongoosejs.com/docs/api/model.html#Model.updateOne() Just add option {timestamps: false} after {$set: {}} update query like
|
@donoftime2018 Appreciate your input. However, as mentioned, I'm reluctant to disable the timestamps option entirely. timestamps shouldn't be updated when no field are update at all, this is the point of the issue. I will edit the example, to show all the use cases. |
If you read the docs, this keeps the timestamps for the schema, but skips the timestamp for the update query. |
What I tried doing was making a pre schema for updateOne and checking if city and url were null, undefined or empty string , or check that they equal the current url/city. If so, then keep the update.updatedAt = this.updatedAt.
|
I am sorry @donoftime2018 but if you really think this a solution. This is one of the most ugliest work around I have ever seen. I am not looking for this kind of solution nor a solution at all. |
@vkarpov15 I don't think, this need to be fix anymore, since the repo script has an error since there is no way to know the original doc before update, we don't want to do 1 find and 1 update. |
In the |
const mongoose = require("mongoose");
const eventSchema = new mongoose.Schema(
{ url: String, city: String },
{ timestamps: true }
);
const Event = mongoose.model("Event", eventSchema);
async function run() {
await mongoose.connect("mongodb://localhost:27017");
await mongoose.connection.dropDatabase();
await Event.create({ url: "google.com" });
const res = await Event.updateOne({ url: "google.com" }, { $set: { url: "google.com" } });
console.log(res); // should not show { modifiedCount: 1 }, neither update updateAt field, { timestamp: false } is not an solution
const res2 = await Event.updateOne({ url: "google.com" }, { $set: { url: "yahoo.com" } });
console.log(res2); // this is OK to update updatedAt field since the docs is in fact updated
}
run(); |
I took a look and it looks like we should punt this to Mongoose 9, because right now Mongoose always increments |
I think this is normal behavior because it is your initiative to update the document |
@vkarpov15 I'd like to help with adding this feature to mongoose 9. |
@guda-art you have a point, but for the sake of argument, shouldn't @donoftime2018 this feature is currently more of a discussion, we're not committing to making |
In my opinion it makes sense to update the timestamp, as we have called an update on the document, no matter if it's the same data it had before; And I think most of the developers are used to see this as updated. So I don't suggest changing it as many developers are using this feature with this mindset. |
@abarriel @hasezoey @AbdelrahmanHafez @IslandRhythms what do you think? @Imanghvs thanks for your thoughts. Why do you think most developers are used to this behavior - is it because Mongoose has had this behavior for so long, or is it because other libs do something similar? |
What do you think everyone? |
It's because Mongoose has had this behavior for a long time. P.S. I totally agree with @AbdelrahmanHafez |
I agree with every point made by @AbdelrahmanHafez. |
i also agree with @AbdelrahmanHafez, as in that as long as a command is send we should update the |
To some extent, the "right" answer here depends on your use case for performing updates. Imagine a situation where you're keeping a document synced up with an external system's data. You would want I see both sides, but ultimately it feels cleaner to maintain existing behavior because Ultimately seems like this issue is more about a theoretical "changedAt" property, which explicitly is about changes. |
Prerequisites
Mongoose version
8.0.4
Node.js version
16
MongoDB server version
6.0.6
Typescript version (if applicable)
No response
Description
Hi,
When updating a doc for a schema that includes the
{ timestamp: true }
option, I've noticed that even when no updated fields are found, theupdatedAt
field still gets updated.In my opinion, this shouldn't be the case since the doc are not updated.
Same things when updating the doc when the fields have the same values as the original ones.
Disabling timestamps is not an option in this particular update, since most of the time the update does in fact really update the doc with new fields.
Thank you
Steps to Reproduce
Expected Behavior
No response
The text was updated successfully, but these errors were encountered: