I am experiencing really weird behavior when trying to update a single record. I use UpdateOne() method and it works as expected in 99% of cases BUT sometimes I get the following results:
As you can see, MongoDB was able to find my record but it wasn't updated. I've tried to change write concern, which accordingly to the docs, might help:
collection.WithWriteConcern(WriteConcern.WMajority.With(journal: true))
but it didn't.
Here is how I update my records:
collection.UpdateOne(x => x.Id == _myObject.Id.AsObjectId, updateDef);
Update definition:
var updateDef = new UpdateDefinitionBuilder<IndexedProfileData>().Set(x => x.Property.ChildCollection, newCollection);
I would be really appreciated if somebody could explain me why this is happening and how to fix this behavior.
MongoDB will not update a document if it's already in its "updated" state.
For example, using the mongo
shell:
> db.test.find()
{"_id": 0, "a": 0}
> db.test.update({_id:0}, {$set:{a:1}})
WriteResult({
"nMatched": 1,
"nUpserted": 0,
"nModified": 1
})
Since a
is 0
and we're setting a
to 1
, the update modified the document (nMatched: 1
, nModified: 1
).
> db.test.find()
{"_id": 0, "a": 1}
> db.test.update({_id:0}, {$set:{a:1}})
WriteResult({
"nMatched": 1,
"nUpserted": 0,
"nModified": 0
})
If we're trying to set a
to 1
again, the update statement found the document, but realized that it doesn't need to do any work (nMatched: 1
, nModified: 0
).