Will mongodb $set produce dirty data when updating different fields of the same document?

mongodb $set updates different fields of the same document, resulting in dirty data?
High concurrency may update different fields at the same time, such as two people updating at the same time, A updating field one, B updating field two. Is it possible that A updates while B updates, and then A updates the fields back? There is an article saying that $set is to update a field, and this operation is atomic. But I didn"t see this description in more places or more authoritative places. It"s not going to cause the problem I mentioned above, is it?

mongoDB modifier $set by lockymeng
probably the following two sentences execute
db.users.update ({" name ":" mfw "}, {" $set ": {" addr ":" shandong "}})
db.users.update ({" name ":" mfw "}, {" $set ": {" mobile ":" 1333333333 "}})

Correction. It has been confirmed that it is an atomic operation. I didn"t look at it carefully, but I still wonder whether it affects updating different fields at the same time

.
Mar.02,2021

mongo's writing to a single document is always atomic.
in fact, the subject wants an authoritative statement, and there is certainly nothing more authoritative than an official document:

In MongoDB, a write operation is atomic on the level of a single document, even if the operation modifies multiple embedded documents within a single document.

When a single write operation modifies multiple documents, the modification of each document is atomic, but the operation as a whole is not atomic and other operations may interleave.

in mongo, the write operation of a single document is always atomic, even if multiple nested documents in a single document are updated at the same time.

when an operation updates multiple documents at the same time, the write operation for each single document is atomic, but the whole batch update operation is not atomic, and other operations may be staggered.

reference: mongo official documentation-Atomicity and Transactions

Menu