0

We need to create an atomic routine in our MongoDB database.

We need to iterate through a collection, find the highest number given a field from all documents in the collection, then increment it. We are working with some legacy data that we need to integrate, otherwise we'd have some atomic sequence already in place.

How can I create stored JS or a stored procedure in MongoDB that can run a whole routine atomically?

I am seeing some information but nothing is looking particularly clear to me:

Called a stored javascript function from Mongoose?

https://groups.google.com/forum/#!topic/mongoose-orm/sPN3wfDstX4

https://github.com/mongoosejs/mongoose-function

Where can I find good information how to actually write an atomic/blocking stored procedure that runs in MongoDB, and how to actually invoke the stored procedure from the application?

15
  • 1
    There's no such thing as atomic/blocking stored procedure in mongodb. What you want to do may be possible with findAndModify, but that's a) really hard to tell, given how vague your description is and b) is atomic only at document level. Commented Jul 25, 2017 at 20:27
  • @SergioTulentsev it's not a vague question at all, I described exactly what I want to do :) Well, what I want to do more exactly, is to create a stored procedure that can create a lock on a document and hold onto the lock for the lifetime of the stored procedure. Commented Jul 25, 2017 at 20:34
  • Yeah, not gonna happen :) Commented Jul 25, 2017 at 20:35
  • 1
    I wrote this, if you are interested - github.com/ORESoftware/live-mutex Commented Jul 25, 2017 at 20:44
  • 1
    In a real ACID db, you don't have to agree to follow the rules. The rules are enforced by the database. Just saying. :) If this works for you, then great, my job here is done :) Commented Jul 25, 2017 at 20:46

1 Answer 1

1

(summarizing the comments above)

At the moment, there is nothing in mongodb that will allow you to run a piece of arbitrary logic (including, for example, multiple queries to gather data) atomically.

The best atomic thing that mongodb has to offer is findAndModify. Its atomicity is naturally restricted to only one document and you have a pretty limited list of update operators (that is, you can't even use the fields of the document, same as regular updates).

It is somewhat possible using an application-level lock: the application inserts or modifies a special lock document, which will signal to other parts of the application "I'm using/updating this, please refrain from touching it". After the operation is completed, application releases the lock, so it's now free to be re-acquired by someone else. Of course, this relies entirely on actors respecting the lock agreements, which is not very reliable, to put it mildly.

Sign up to request clarification or add additional context in comments.

11 Comments

TTL indexes "work" for locking with MongoDB but I don't know how well they really work
@AlexanderMills: TTL index (in the slide deck you reference) is only there to ensure lock release if, for whatever reason, your program crashes before it can release the lock normally. Other than this, they don't have anything to do with locking, whatsoever.
No not true, the lock holder acquires the lock by setting the index with ttl, when the lockholder is done, the lockholder deletes the index, system does not have to wait til index expiry..the other lock requestors using polling to acquire the lock, they acauire the lock when they successfully create the index, when they are done they delete it. ttl is simply the critical feature needed if application goes down. Its a crappy locking system because it requires lots of polling but it will work.
@AlexanderMills: what do you mean "they create the index"? No indexes are dynamically created/deleted at runtime in that scenario, I assure you. :)
@AlexanderMills: yep, with that I can agree.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.