Mongodb autoincrement

Hi all

Has anyone got a cunning way way of implementing an atomic autoincrement eg
the example in mongoDB docs
http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/?

There is something about this in the Railo
https://groups.google.com/forum/#!msg/railo/1E9wcG1SwYI/wUrBzNok1yoJ 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
<http://api.mongodb.org/java/2.11.4/com/mongodb/DBCollection.html#findAndModify(com.mongodb.DBObject,
com.mongodb.DBObject, com.mongodb.DBObject, boolean, com.mongodb.DBObject,
boolean, boolean)> 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

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, JonOn September 25, 2015 at 2:17:34 AM, Richard Meredith-Hardy (@Richard_Meredith-Har) wrote:

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

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+unsubscribe@googlegroups.com.
To post to this group, send email to lucee@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.

You could always access Mongo shell in your application CFML using 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.On September 26, 2015 at 6:03:45 AM, Richard Meredith-Hardy (@Richard_Meredith-Har) wrote:

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

On Friday, 25 September 2015 14:28:37 UTC+1, Jon Clausen wrote:
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

On September 25, 2015 at 2:17:34 AM, Richard Meredith-Hardy (r....@flymicro.com) wrote:

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

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.

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+unsubscribe@googlegroups.com.
To post to this group, send email to lucee@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lucee/edaa425c-7903-4681-b62d-5bf2014559cd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

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
http://www.google.com/url?q=http%3A%2F%2Fdocs.mongodb.org%2Fmanual%2Ftutorial%2Fcreate-an-auto-incrementing-field%2F&sa=D&sntz=1&usg=AFQjCNE40SEr8P0smCDvx07K97KqSJS7aw.
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

RichardOn Friday, 25 September 2015 14:28:37 UTC+1, Jon Clausen wrote:

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

On September 25, 2015 at 2:17:34 AM, Richard Meredith-Hardy ( r....@flymicro.com <javascript:>) wrote:

Hi all

Has anyone got a cunning way way of implementing an atomic autoincrement
eg the example in mongoDB docs
http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/?

There is something about this in the Railo
https://groups.google.com/forum/#!msg/railo/1E9wcG1SwYI/wUrBzNok1yoJ 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
http://api.mongodb.org/java/2.11.4/com/mongodb/DBCollection.html#findAndModify(com.mongodb.DBObject,%20com.mongodb.DBObject,%20com.mongodb.DBObject,%20boolean,%20com.mongodb.DBObject,%20boolean,%20boolean)
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

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 <javascript:>.
To post to this group, send email to lu...@googlegroups.com <javascript:>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/lucee/cc546489-f94a-49b4-af42-0de5bd94f4e0%40googlegroups.com
https://groups.google.com/d/msgid/lucee/cc546489-f94a-49b4-af42-0de5bd94f4e0%40googlegroups.com?utm_medium=email&utm_source=footer
.
For more options, visit https://groups.google.com/d/optout.