MongoDB PHP 驱动程序:对存储的 JS 使用执行 [英] MongoDB PHP Driver: Using Execute for Stored JS
问题描述
我创建了一个 Stored JS 函数,据说它可以更快、更高效地完成一项非常乏味的工作.现在经过长时间的投入工作,我想出了完成这个功能.
I created a Stored JS function which supposedly completes a very tedious job, faster and efficiently. Now after a investing long working hours I came up with completing this functionality.
所以我的函数是假设 myFunc();
So my function is suppose myFunc();
db.system.js.save(
{
_id : "myFunc" ,
value : function (param1,param2,param3....){ ... }});
一旦我完成了这个功能,并使用 Mongo Shell 进行测试
Once I was through with completing this function, and testing it with Mongo Shell as
db.eval("myFunc('a','b'...)");
和
db.loadServerScripts();
myFunc('a','b',..);
我很满意它会起作用,现在问题开始了,如何用PHP来实现这一点?
I was satisfied it will work, Now the problem begins, How to achieve this with PHP?
$cmd = 'db.eval("myFunc(\'a\',\'b\'...);");';
$data = $mongo_db->execute($cmd);
var_dump($data);
$data 一无所获..!!
$data comes up with nothing..!!
array(2) { ["retval"]=> NULL ["ok"]=> float(1) }
请有人告诉我我到底做错了什么..!!
Somebody tell me please what the heck am I doing wrong..!!
推荐答案
我发现的简单解决方法:
The Simple workaround I found:
var out = db.eval("abc(param1,param2..)"); return(JSON.stringify(out));
我知道它根本不是实现这一目标的可取方法,但直到我找到更好的解决方法(因为存储 JS 不是一种可取的方法)它是我唯一的方法.如有任何失败,将发布.
I know its not at all advisable way to achieve this, but till the time I find a better workaround( as Stored JS is not an advisable method ) its the only way I have. Will post in case of any failure.
这篇关于MongoDB PHP 驱动程序:对存储的 JS 使用执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!