mongodb autoincrement

78 views
Skip to first unread message

Richard Meredith-Hardy

unread,
Sep 25, 2015, 2:17:31 AM9/25/15
to Lucee
Hi all

Has anyone got a cunning way way of implementing an atomic autoincrement eg the example in mongoDB docs?   

There is something about this in the Railo group, but as per the comment there, using the 'full' findAndModify syntax seems to return the error the function findAndModify only support up to 3 arguments, but you have defined 7 and neither of the other options returns the new document.

I'm on a recent install of Lucee 4.5.1.023 final, mongoDB 3 and the mongoDB extension

Thanks in advance

Richard

Jon Clausen

unread,
Sep 25, 2015, 9:28:37 AM9/25/15
to lu...@googlegroups.com

Can you post a code sample of how you are trying to implement this that is failing? Are you trying to create the sequence function using the Java driver? If so, you’ll need to create the that function in Mongo shell. Either way, with the Java driver, you’re going to need to call it using

db.eval('getNextSequence("collectionid")’).

If you absolutely have to have the sequencing as your _id value (which Mongo advises against in the docs), you could also handle that at the application level with a sequence finder function:

function getNextId(){
     
    var maxId = db.getCollection("myCollection").aggregate({
            "$group": {
                "_id": '',
                "last": {
                    "$max": "$_id"
                }
            }
    }).asArray()[1].last;
     
    return maxId + 1;
}

This will save you from having to use db.eval() and, since you’re going to have to pass an _id value explicitly in every insert query, it will save you a few keystrokes (albeit an extra call to the db). You can also add some locks around the insert to ensure a unique sequence value, which the Mongo docs note the findAndModify() method is vulnerable to failing on concurrent inserts.

HTH, Jon

--
See Lucee at CFCamp Oct 22 & 23 2015 @ Munich Airport, Germany - Get your ticket NOW - http://www.cfcamp.org/
---
You received this message because you are subscribed to the Google Groups "Lucee" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lucee+un...@googlegroups.com.
To post to this group, send email to lu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lucee/cc546489-f94a-49b4-af42-0de5bd94f4e0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Richard Meredith-Hardy

unread,
Sep 26, 2015, 5:52:39 AM9/26/15
to Lucee
thanks for the reply

Yes I was aware of the mongodb advice about autoincrements but in this use case (assets) there will never be squillions of them and it would be very useful to be able to order them by an obvious form of id.  (other collections save data from assets which will potentially become squillions)

Before you replied, my work-around was to simply do 2 calls to the db, increment the value and then retrieve it, both surrounded by an exclusive cflock.  Since new assets will be relatively rare the penalty of two calls should be minimal.

Nevertheless, it's always nice to do things efficiently, and you provide a clue that it's not so difficult to access the mongodb shell directly and create and run shell functions like getNextSequence(name) as explained in the mongoDB docs.  I have no idea how one might do this in Lucee so if you could point me to some resources which might give me a clue I'd be very grateful.

Thanks

Richard

Jon Clausen

unread,
Sep 26, 2015, 9:13:16 AM9/26/15
to lu...@googlegroups.com

You could always access Mongo shell in your application CFML using cfexecute()/<cfexecute>. Since you only need to create that function once for the DB, though, it might be simpler and cleaner just to open up a terminal, type in mongo, create your function and then just access it through the Java driver/Lucee plugin by calling db.eval('getNextSequence("mysequenceid")').

If you’re going to use cfexecute, then I’d suggest writing a companion shell script that handles the opening and closing of the mongo shell and accepts the command you want to run.

Reply all
Reply to author
Forward
0 new messages