鍍金池/ 問答/數(shù)據(jù)庫  HTML/ mongodb $set更新同一文檔不同字段,會產生臟數(shù)據(jù)么

mongodb $set更新同一文檔不同字段,會產生臟數(shù)據(jù)么

mongodb $set更新同一文檔不同字段,會產生臟數(shù)據(jù)么
高并發(fā)下可能同時更新不同字段,比如倆人同時更新,A更新字段一,B更新字段二。會不會A在更新的同時B也在更新然后A更新的字段又回去了。有篇文章說$set是更新某個字段,這個操作是原子的。但是我沒有在比較多的地方或者比較權威的地方看到這個描述。不會產生上邊我說的這個問題吧?

mongoDB修改器之$set by lockymeng
大概就是下邊倆句同時執(zhí)行
db.users.update({"name":"mfw"},{"$set":{"addr":"shandong"}})
db.users.update({"name":"mfw"},{"$set":{"mobile":"13333333333"}})

更正一下,已經確認是原子操作,是我看的不仔細,但是我還是有點疑惑同時更新不同字段有影響么

回答
編輯回答
款爺

mongo 對單一文檔的寫操作總是原子的.
其實題主是想要一個權威的說法, 肯定沒有什么比官方文檔更權威了:

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.

在 mongo 中, 單個文檔的寫操作總是原子的, 即使同時更新單一文檔中的多個嵌套文檔也是如此.

當一個操作同時更新了多個文檔的時候, 該操作對每個單一文檔的寫操作是原子的, 但整個批量更新操作并不是原子的, 其他操作有可能會被交錯執(zhí)行.

參考: mongo 官方文檔 - Atomicity and Transactions

2017年1月3日 09:40