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.
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/lucee/edaa425c-7903-4681-b62d-5bf2014559cd%40googlegroups.com.