【问题标题】:Calling stored JavaScript function in MongoDB from PHP从 PHP 调用存储在 MongoDB 中的 JavaScript 函数
【发布时间】:2010-10-14 22:27:49
【问题描述】:

我有这个函数存储在 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;
    }
}

在 PHP 中

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

错误

Array
(
    [errno] => -3
    [errmsg] => invoke failed: JS Error: ReferenceError: o is not defined nofile_a:0
    [ok] => 0
)

【问题讨论】:

  • $db->e​​xecute( new MongoCode('AutoID(o)', array( 'o' => array('name' => 'test') ) ) );
  • 很高兴看到你已经解决了。随时发布您的解决方案作为答案并接受它。这样问题就不会看起来像没有答案:)

标签: php mongodb java-stored-procedures mongodb-php


【解决方案1】:
$db->execute(
    new MongoCode('SomeFunc(o)', array(
        'o' => array('name' => 'test') 
    ))
);

【讨论】:

    猜你喜欢
    • 2016-10-09
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-13
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    • 2020-03-16
    相关资源
    最近更新 更多