1

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: enter image description here 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.

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Aha! I thought about this as well but wasn't sure. Thx a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.