Answer the question
In order to leave comments, you need to log in
Calling Stored JavaScript Functions in MongoDB from PHP
We have a function saved in db.system.js with the name AutoID
From the console, everything works fine
But I still don’t understand how to call this miracle in PHP, can someone explain / show?
function (n,o) {
x = db.getCollection(n);
while( 1 ) {
var c = x.find({},{id:1}).sort({id:-1}).limit(1);
var i = c.hasNext() ? c.next().id + 1 : 1;
o.id = i;
x.insert(o);
var err = db.getLastErrorObj();
if( err && err.code ) {
if( err.code == 11000 /* dup key */ )
continue;
else
print("unexpected error inserting data: " + tojson(err));
}
break;
}
}
db.eval("AutoID('Users',{'name':'Tenkoff'})");
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question