[SERVER-12329] UpdatedExisting always true after update command Created: 10/Jan/14  Updated: 25/May/22  Resolved: 15/Jan/14

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: 2.5.4
Fix Version/s: 2.5.5

Type: Bug Priority: Major - P3
Reporter: Bernie Hackett Assignee: Randolph Tan
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-12474 mongos gle no longer returns updatedE... Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Participants:

 Description   

getLastError appears to always return true for updatedExisting when the last operation was an update, regardless of whether a document was updated or not.

> db.test.remove()
SingleWriteResult({
	"writeErrors" : [ ],
	"writeConcernErrors" : [ ],
	"nInserted" : 0,
	"nUpserted" : 0,
	"nUpdated" : 0,
	"nModified" : 0,
	"nRemoved" : 2,
	"upserted" : [ ]
})
> db.test.insert({i: 1})
SingleWriteResult({
	"writeErrors" : [ ],
	"writeConcernErrors" : [ ],
	"nInserted" : 1,
	"nUpserted" : 0,
	"nUpdated" : 0,
	"nModified" : 0,
	"nRemoved" : 0,
	"upserted" : [ ]
})
> db.test.update({i: 5}, {$set: {i: 500}})
SingleWriteResult({
	"writeErrors" : [ ],
	"writeConcernErrors" : [ ],
	"nInserted" : 0,
	"nUpserted" : 0,
	"nUpdated" : 0,
	"nModified" : 0,
	"nRemoved" : 0,
	"upserted" : [ ]
})
> db.runCommand('getLastError')
{
	"updatedExisting" : true,
	"n" : 0,
	"connectionId" : 2291,
	"syncMillis" : 0,
	"writtenTo" : null,
	"err" : null,
	"ok" : 1
}
> db.test.find()
{ "_id" : ObjectId("52d07a4cd05c85fb8d3f5e3d"), "i" : 1 }
> db.test.update({i: 5}, {$set: {i: 1}})
SingleWriteResult({
	"writeErrors" : [ ],
	"writeConcernErrors" : [ ],
	"nInserted" : 0,
	"nUpserted" : 0,
	"nUpdated" : 0,
	"nModified" : 0,
	"nRemoved" : 0,
	"upserted" : [ ]
})
> db.runCommand('getLastError')
{
	"updatedExisting" : true,
	"n" : 0,
	"connectionId" : 2291,
	"syncMillis" : 0,
	"writtenTo" : null,
	"err" : null,
	"ok" : 1
}
> db.test.find()
{ "_id" : ObjectId("52d07a4cd05c85fb8d3f5e3d"), "i" : 1 }



 Comments   
Comment by Samuel Cadieux [ 25/May/22 ]

I've noticed a similar issue on 5.0.6 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

 

I've created this issue to track this issue independently: https://jira.mongodb.org/browse/SERVER-66740

Comment by Githook User [ 15/Jan/14 ]

Author:

{u'username': u'renctan', u'name': u'Randolph Tan', u'email': u'randolph@10gen.com'}

Message: SERVER-12329 UpdatedExisting always true after update command
Branch: master
https://github.com/mongodb/mongo/commit/7a0aa7d3b23ddae748894806e4fa2a6949a5d7c1

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