4

I have this function stored in db.system.js

function SomeFunc(o) {
    x = db.Collection;
    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;
    }
}

In PHP

print_r(
    $db->execute("SumeFunc(o)", array("name" => "test"))
);

Error

Array
(
    [errno] => -3
    [errmsg] => invoke failed: JS Error: ReferenceError: o is not defined nofile_a:0
    [ok] => 0
)
2
  • $db->execute( new MongoCode('AutoID(o)', array( 'o' => array('name' => 'test') ) ) ); Commented Oct 15, 2010 at 1:11
  • Good to see you've solved it. Feel free to post your solution as an answer and accept it. That way the question won't look like it's unanswered :) Commented Oct 15, 2010 at 8:38

1 Answer 1

8
$db->execute(
    new MongoCode('SomeFunc(o)', array(
        'o' => array('name' => 'test') 
    ))
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.