How does MongoDB deal with concurrent updates?

岁酱吖の 提交于 2019-11-26 07:43:42

问题


I started to use MongoDB at work so far so good. I was wondering though how does MongoDB deal with concurrent updates ? I\'ve read that there is no locking feature in MongoDB so I was wondering what is the the common practice to deal with this.

Thanks.


回答1:


MongoDB used a process wide write lock to guarantee that only one write operation (update/insert/remove) can be performed at a time. As such it automatically solves concurrency issues since write concurrency simply isn't allowed.

If 4 threads attempt an update operation one of them will take the write lock, do its update and release the lock. After that one of the remaining 3 will grab the lock, do its update, etc.

Concurrency only comes into play if your operation cannot be wrapped in a single write operation. Note that for the most common usecase (find a doc, update it and grab the new version atomically) MongoDB offers the "findAndModify" command which does just that : http://www.mongodb.org/display/DOCS/findAndModify+Command

UPDATE : Locking is more granular these days.




回答2:


Use modifier operations:

$inc $set $unset $push $pushAll $addToSet $pop $pull $pullAll $rename $bit

all of them are atomic.



来源:https://stackoverflow.com/questions/6997835/how-does-mongodb-deal-with-concurrent-updates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!