...
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.
xgen-internal-githook commented on Fri, 29 Jan 2016 22:38:11 +0000: 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 david.storch commented on Wed, 9 Dec 2015 20:30:02 +0000: 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. rassi@10gen.com commented on Wed, 9 Dec 2015 20:17:31 +0000: 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?