Calling a stored procedure via PHP in MongoDB

試著忘記壹切 提交于 2019-12-10 11:05:42

问题


Dear everybody who can help,

I have this PHP > MongoDB problem, I want to call a stored procedure (stored in db.system.js collection) via PHP.

I have no parameters, only a returning JSON object which looks like this:

{"archived":[the number of the archived messages]}

It works good in the shell on the database server, but when I try to call it via the PHP driver it just doesn't "say" anything...

My code looks like this:

$DB->execute(
    new MongoCode(
        "function () { return archiveMessages(); }"
    )
);

I have also tried to use somehow like this:

$DB->execute("archiveMessages()");

Please help, I've got stuck on this one... I only want to call that sh*t after updating the collection...

Thank you in advance, B


回答1:


Are you sure you're using the same database? Try simplifying it to a basic example, e.g.,

$m = new Mongo();
$db = $m->foo;

$db->system->js->save(array("_id"=>"archiveMessages", 
   "value"=>new MongoCode("function() { return 3; }")));

print_r($db->execute("archiveMessages()"));

Results in:

Array
(   
    [retval] => 3
    [ok] => 1
)



回答2:


Check the documentation:

http://php.net/manual/en/mongodb.execute.php

Are you assigning the return value of execute() to a variable?



来源:https://stackoverflow.com/questions/9716648/calling-a-stored-procedure-via-php-in-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!