BSONJavascript Auto Increment Issue

39 views
Skip to first unread message

Vikas Naidu

unread,
Feb 5, 2016, 3:15:37 AM2/5/16
to ReactiveMongo - http://reactivemongo.org
Hi, 
 
   I'm trying to set a field as auto increment in my collection. I have set the counters and the JavaScript function in mongo.
#counter
  db.counters.insert(
...    {
...       _id: "userid",
...       seq: 0
...    }
... )
#function
function getNextSequenceTest(name) {
...    var ret = db.counters.findAndModify(
...           {
...             query: { _id: name },
...             update: { $inc: { seq: 1 } },
...             new: true,
...             upsert: true
...           }
...    );
... 
...    return ret.seq;
... }

Now in the writer i have used BSONJavascript "user_id" -> BSONJavaScript("getNextSequenceTest(\"userId\")"))

it's being inserted. when i read it in the console
 db.infocards.find({})
 {"user_id":1}

if i do it again 
 db.infocards.find({})
 {"user_id":2}

 db.infocards.find({})
 {"user_id":3}
 
 db.infocards.find({})
 {"user_id":4}

for every find the javascript function is being called. 

Also if read the document from reactivemongo, field can be read as BSONJavascript not the integer value.

if i insert a doc from shell it's giving a normal behavior

Can anyone tell me what am i doing wrong.

Cédric Chantepie

unread,
Feb 5, 2016, 5:56:54 PM2/5/16
to ReactiveMongo - http://reactivemongo.org
For me, it has more to do with the way MongoDB handles server-side JS, which must not be confused with MongoDB shell specificity.
Reply all
Reply to author
Forward
0 new messages