[SERVER-21823] Update / findAndModify with {upsert: true} should report nMatched:0 nModified:0 if results in an insert Created: 09/Dec/15  Updated: 22/Mar/16  Resolved: 29/Jan/16

Status: Closed
Project: Core Server
Component/s: Diagnostics, Write Ops
Affects Version/s: 2.6.11, 3.0.7, 3.2.0
Fix Version/s: 3.3.2

Type: Bug Priority: Major - P3
Reporter: David Storch Assignee: Tess Avitabile (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Operating System: ALL
Sprint: Query F (02/01/16)
Participants:

 Description   

Consider an update operation with the {upsert: true} flag whose query predicate does not match any of the documents in the collection:

db.setLogLevel(1);
db.c.drop();
db.c.insert({a: 1});
db.c.update({a: 2}, {$set: {b: 1}}, {upsert: true});

This update operation does not match any documents, nor does it modify any existing documents, but in nonetheless generates the following log line:

2015-12-09T14:06:02.569-0500 I WRITE    [conn1] update test.c query: { a: 2.0 } update: { $set: { b: 1.0 } } keysExamined:0 docsExamined:1 nMatched:1 nModified:1 upsert:1 keyUpdates:0 writeConflicts:0 numYields:0 locks:{ Global: { acquireCount: { r: 1, w: 1 } }, Database: { acquireCount: { w: 1 } }, Collection: { acquireCount: { w: 1 } } } 0ms

Instead of nMatched:1 and nModified:1 this should report nMatched:0 and nModified:0. This applies to both update and findAndModify for both the log and the profile collection.



 Comments   
Comment by Githook User [ 29/Jan/16 ]

Author:

{u'username': u'tessavitabile', u'name': u'Tess Avitabile', u'email': u'tess.avitabile@mongodb.com'}

Message: SERVER-21823 Update and findAndModify with

{upsert: true}

should report nMatched:0 nModified:0 if results in an insert
Branch: master
https://github.com/mongodb/mongo/commit/e40a6da919084cba4b0397c1da21c6ee93e78297

Comment by David Storch [ 09/Dec/15 ]

This is not a regression. I believe the problem in some form affects at least 2.6, 3.0, and 3.2. In fact, I purposefully preserved this behavior at one point during 3.0 development since I was working on a pure refactor for explain:

https://github.com/mongodb/mongo/blob/d1224336faea6e0dbc46e676cd23e94387c220be/src/mongo/db/exec/update.cpp#L1045-L1052

It looks like UpdateResult is actually correct, but this is only because there is some crazy accounting that the write commands do:

https://github.com/mongodb/mongo/blob/d1224336faea6e0dbc46e676cd23e94387c220be/src/mongo/db/commands/write_commands/batch_executor.cpp#L1235-L1237

I have not checked on the findAndModify command response or the GLE response.

Comment by J Rassi [ 09/Dec/15 ]

Dave, I have a couple of follow-up questions:

  • Is the update result (either in the update command response, fAM command response, or GLE response) also similarly incorrect?
  • Is this a regression from an earlier version of the server?
Generated at Thu Feb 08 03:58:32 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.