1

Using the mongo shell, I am able to find a document.

However, when doing a no-op update, no document is matched, and also for some reason, somewhere, a document fails validation.

How is that possible? What could be happening? Am I missing something?

> db.synchronousEvents.count({_id: ObjectId('5dd82343a23aa2b0d5a147cf')})
1
> db.synchronousEvents.update({_id: ObjectId('5dd82343a23aa2b0d5a147cf')}, {})
WriteResult({
    "nMatched" : 0,
    "nUpserted" : 0,
    "nModified" : 0,
    "writeError" : {
        "code" : 121,
        "errmsg" : "Document failed validation"
    }
})
Nino Filiu
  • 16,660
  • 11
  • 54
  • 84

1 Answers1

1

nMatched is at 0 because that's the expected behavior for any write which throws.

See test:

//
// Update with error
coll.remove({});
coll.insert({foo: "bar"});
printjson(result = coll.update({foo: "bar"}, {$invalid: "expr"}));
assert.eq(result.nUpserted, 0);
assert.eq(result.nMatched, 0);
if (coll.getMongo().writeMode() == "commands")
    assert.eq(0, result.nModified, tojson(result));
assert(result.getWriteError());
assert(result.getWriteError().errmsg);
assert(!result.getUpsertedId());
assert.eq(coll.count(), 1);

https://github.com/mongodb/mongo/blob/b7ff5816f4d9d468b1875013384e7e51184628a0/jstests/core/write_result.js#L116

And the write throws because {} isn't a valid document for the collection synchronousEvents.

L. Sanna
  • 6,482
  • 7
  • 33
  • 47