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":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.