来自多个客户端的MongoDB findAndModify [英] MongoDB findAndModify from multiple clients

查看:64
本文介绍了来自多个客户端的MongoDB findAndModify的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MongoDB集合用作作业队列,并且有3台C ++计算机从该集合中读取.问题在于这三个人不能执行相同的工作.所有工作只需完成一次.

my MongoDB collection is used as a job queue, and there are 3 C++ machines that read from this collection. The problem is that those three can't perform the same job. All jobs need to be made only once.

我通过使用'isDone:False'在集合中搜索所有记录来获取所有未完成的作业,然后更新此文档'isDone:True'.但是,如果两台机器同时找到相同的文档,则它们将同时完成相同的工作.如何避免这种情况?

I fetch all un-done jobs by searching the collection for all records with 'isDone:False' and then update this document, 'isDone:True'. But if 2 machines find the same document at the same time, they would to the same job both. How can I avoid this?

我的问题是-findAndModify确实可以解决该问题吗? (阅读确保在MongoDb的findAndModify中进行独占读取的方法?)

My question is - do findAndModify really solves that problem? (After reading A way to ensure exclusive reads in MongoDb's findAndModify?)

推荐答案

是的,findAndModify解决了它.

Yes, findAndModify solve it.

引用:从多个客户端的MongoDB findAndModify "... 注意:该命令在受影响的数据库上获得了写锁定,并将阻止其他操作,直到完成为止;但是,写锁定通常是短暂的,并且等效于其他类似的update()操作. ..."

Ref: MongoDB findAndModify from multiple clients "... Note: This command obtains a write lock on the affected database and will block other operations until it has completed; however, typically the write lock is short lived and equivalent to other similar update() operations. ..."

参考: http://docs.mongodb.org/manual/reference/method/db.collection.update/#db.collection.update "... 对于未分片的集合,您可以使用$ isolated隔离运算符覆盖此行为,该运算符隔离更新操作并在更新过程中阻止其他写入操作.请参阅隔离运算符. ..."

Ref: http://docs.mongodb.org/manual/reference/method/db.collection.update/#db.collection.update "... For unsharded collections, you can override this behavior with the $isolated isolation operator, which isolates the update operation and blocks other write operations during the update. See the isolation operator. ..."

参考: http://docs.mongodb.org/manual/reference/运算符/isolated/

关于, Moacy

这篇关于来自多个客户端的MongoDB findAndModify的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆