[SERVER-66740] findAndModify.lastErrorObject.updatedExisting always true after and update Created: 25/May/22 Updated: 01/Jul/22 Resolved: 03/Jun/22 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | None |
| Affects Version/s: | 5.0.6 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Samuel Cadieux | Assignee: | Chris Kelly |
| Resolution: | Duplicate | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||||||
| Operating System: | ALL | ||||||||||||||||
| Steps To Reproduce: |
|
||||||||||||||||
| Participants: | |||||||||||||||||
| Description |
|
I've noticed an issue with findAndModify, where updatedExisting always return true when updating a field with the same value. I found this blog post that describe the issue well: https://codehunter.cc/a/mongodb/findandupdate-how-to-check-if-document-was-really-updated Although this behavior seems to be a bug, it's in line with the documention: https://www.mongodb.com/docs/manual/reference/command/findAndModify/#lasterrorobject However, I would expect updatedExisting to be true if the document was upserted or if any modification was done to the existing document. Similar to how the update command output: https://www.mongodb.com/docs/manual/reference/command/update/#mongodb-data-update.nModified Might be related to this other issue: https://jira.mongodb.org/browse/SERVER-12329 |
| Comments |
| Comment by Samuel Cadieux [ 03/Jun/22 ] |
|
Hi Chris, indeed SERVER-28145 from 2017 would solve this issue, but I'm hoping the priority on this issue would be increased. We currently don't have a reliable way to tell what whether the result of a findAndModify operation was a no-op and we rely on this information in our system to notify state updates. We've identified a few workarounds, but none is perfect or reliable. Can you think of a better workaround in the meantime? Workaround 1: Use update instead of findAndModify This is an acceptable workaround if we query the document using a unique index. However, problems arise if we need to update a document using a filter that is not unique. Workaround2: Client-side comparison |
| Comment by Chris Kelly [ 03/Jun/22 ] |
|
Hi Samuel, Thanks for your report. According to the documentation, updatedExisting will be true if either of the following occur:
In this case, when you first create the document this will be false, but running the findAndModify query while the document is in the desired destination state already will cause it to return true. Adding extra information indicating whether changes were made (like nModified) is on the backlog in SERVER-28145 Regards, |
| Comment by Samuel Cadieux [ 25/May/22 ] |
|
To reproduce, run the following command twice using mongosh: Notice how the output of the second execution returns: |