I have multiple servers sharing a common mongodb. In the DB there is a list of jobs, the servers have to finish. As I want to divide the load over all servers and want to avoid multiple servers doing the same job, I want to "Lock" that job.
My Idea is:
Set the element as taken if it is not yet taken:
db.collection.update({done: false, taken: false},{$set: {taken: true, takenBy: myIp}});
Check if the server got the mutex on this element:
db.collection.findOne({taken: true, takenBy: myIp})
Would that be the best way to "synchronize" multiple worker servers over a mongodb (does the server do updates in a single transaction like mysql) or could the server do multiple of this first commands at once?