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

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

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

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

更正一下,已經(jīng)確認(rèn)是原子操作,是我看的不仔細(xì),但是我還是有點(diǎn)疑惑同時(shí)更新不同字段有影響么

回答
編輯回答
款爺

mongo 對(duì)單一文檔的寫操作總是原子的.
其實(shí)題主是想要一個(gè)權(quán)威的說法, 肯定沒有什么比官方文檔更權(quán)威了:

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 中, 單個(gè)文檔的寫操作總是原子的, 即使同時(shí)更新單一文檔中的多個(gè)嵌套文檔也是如此.

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

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

2017年1月3日 09:40