MongoDB findAndModify from multiple clients MongoDB findAndModify from multiple clients mongodb mongodb

MongoDB findAndModify from multiple clients


Yes, findAndModify solve it.

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...."

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...."

Ref: http://docs.mongodb.org/manual/reference/operator/isolated/

Regards,Moacy


Yes, find-and-modify will solve your problem:

db.collection.findAndModify( { query: { isDone: false }, update: { $set: { isDone: true } }, new: true, upsert: false # never create new docs} );

This will return a single document that it just updated from false to true.

But you have a serious problem if your C++ clients ever have a hiccup (the box dies, they are killed, the code has an error, etc.) Imagine if your TCP connection drops just after the update on the server, but before the C++ code gets the job. It's generally better to have multi-phase approach:

  • change "isDone" to "isInProgress", then when it's done, delete the document. (Now, you can see the stack of "todo" and "being done". If something is "being done" for a long time, the client probably died.

  • change "isDone" to "phase" and atomically set it from "new" to "started" (and later set it to "finished"). Now you can see if something is "started" for a long time, the client may have died.

If you're really sophisticated, you can make a partial index. For example, "Only index documents with "phase:{ $ne: 'finished'}". Now you don't need to waste space indexing the millions of finished documents. The index only holds the handful of new/in-progress documents, so it's smaller/faster.