[SERVER-4516] findAndModify should tolerate _id in update field if it doesn't modify it Created: 17/Dec/11  Updated: 11/Jul/16  Resolved: 02/Oct/13

Status: Closed
Project: Core Server
Component/s: Write Ops
Affects Version/s: None
Fix Version/s: 2.5.3

Type: Improvement Priority: Major - P3
Reporter: Vincent Côté-Roy Assignee: Scott Hernandez (Inactive)
Resolution: Done Votes: 1
Labels: findAndModify
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-14286 Additional tests for updates and find... Closed
Participants:

 Description   

findAndModify currently throws a "Mod on _id not allowed" error if update contains an _id, even when the query would not actually change the _id of the document, or a new document would be created.

findAndModify instead should only throw an error if it has ascertained that a matched document has a different _id as the one specified in the query.

this applies especially when one uses findAndModify as a way to do a 'save' (ie, an upsert) with a unique key different than _id. in that case the _id would never be changed, but still one is forced to take care of removing the _id from the document when saving it.



 Comments   
Comment by Daniel Pasette (Inactive) [ 03/Oct/13 ]

This appears to be fixed in 2.4.7 already.

> db.version()
2.4.7-pre-
> db.u.find()
{ "_id" : 1, "a" : 1 }
>  db.u.update({_id:1}, {_id:1, a:1}, true)
> db.u.find()
{ "_id" : 1, "a" : 1 }

Comment by Scott Hernandez (Inactive) [ 02/Oct/13 ]

Fixed during the update refactoring in 2.5.3

> db.u.find()
{ "_id" : 1 }
> db.u.update({_id:1}, {_id:1, a:1})
> db.u.find()
{ "_id" : 1, "a" : 1 }
> db.u.update({_id:1}, {_id:1, a:2})
{ "_id" : 1, "a" : 2 }

Generated at Thu Feb 08 03:06:13 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.