开发者

Calling stored JavaScript function in MongoDB from PHP

开发者 https://www.devze.com 2023-01-20 05:12 出处:网络
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);

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("unexp开发者_开发百科ected 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
)


$db->execute(
    new MongoCode('SomeFunc(o)', array(
        'o' => array('name' => 'test') 
    ))
);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号