[SERVER-14535] nModified wrong when replacing document and query on non-ObjectId _id Created: 11/Jul/14  Updated: 26/Apr/19  Resolved: 16/Oct/14

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

Type: Bug Priority: Major - P3
Reporter: A. Jesse Jiryu Davis Assignee: Ramon Fernandez Marina
Resolution: Cannot Reproduce Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Participants:

 Description   

"nModified" should be 0 in the following case, but it's 1:

> db.collection.find()
{ "_id" : 1, "a" : 1 }
> db.runCommand({update: 'collection', updates: [{q: {_id: 1}, u: {_id: 1, a: 1}}]})
{ "ok" : 1, "nModified" : 1, "n" : 1 }

nModified is 0 as expected if:

  • the update uses $set instead of a whole-document replace
  • the _id is an ObjectId instead of an integer
  • the query is on a field besides _id

The behavior is intermittent, as seen in this shell session:

> db.collection.find()
{ "_id" : 1, "a" : 1 }
> db.runCommand({update: 'collection', updates: [{q: {_id: 1}, u: {_id: 1, a: 1}}]})
{ "ok" : 1, "nModified" : 1, "n" : 1 }
> db.collection.drop()
true
> db.collection.insert({_id: 1, a: 1})
WriteResult({ "nInserted" : 1 })
> db.runCommand({update: 'collection', updates: [{q: {_id: 1}, u: {_id: 1, a: 1}}]})
{ "ok" : 1, "nModified" : 0, "n" : 1 }



 Comments   
Comment by Ian Whalen (Inactive) [ 26/Apr/19 ]

Switching "Drivers Changes Needed" from "Maybe" to "Not Needed" since this was closed as something other than Fixed.

Comment by A. Jesse Jiryu Davis [ 16/Oct/14 ]

Can no longer reproduce.

Comment by Ramon Fernandez Marina [ 29/Sep/14 ]

I'm was not able to reproduce this behavior using this loop:

$ cat test.js
db.collection.drop()
db.collection.insert({_id:1, a:1})
for (i = 0; i< 1000; i++) {
    var n = db.runCommand({update: 'collection', updates: [{q: {_id: 1}, u: {_id: 1, a: 1}}]})
    if (n.nModified == 1) {
        print ("Wrong!")
    }
}
$
$ mongo < test.js 
MongoDB shell version: 2.6.3
connecting to: test
true
WriteResult({ "nInserted" : 1 })
bye

jesse, are you still able to reproduce this?

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