0

我正在尝试使用存储的 javascript 在 MongoDB 中存储查询。

现在看起来像这样:

$collection = $db->$app_id;

$query = $collection->find( $filterQuery );

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

然后我执行它:

echo "<pre>";
print_r($test = $db->execute("qo8qu0()"));
echo "</pre>";

但是,我收到此错误: Catchable fatal error: Object of class MongoCursor could not be converted to string in /var/www/dev/classes/EmailMessaging.php on line 91

第 91 行就是这一行(如上), "value" => new MongoCode("function() { return array($filterQuery) }")

如果我使用count( $filterQuery );而不是find( $filterQuery ); 它可以工作并返回正确的数字。

我怎样才能使它工作,以便在使用时返回数组find

谢谢

4

1 回答 1

0

这是因为对您的代码的评估。它正在评估这个:

$query = $collection->find( $filterQuery );

所以这$query是一个MongoCursor然后你试图将该类连接成一个字符串:

new MongoCode("function() { return array($query) }")

您需要像这样在 eval 内完成所有操作:

return $this->execute('function(){ return db.collection.find(filter); }',  array('filter'=>array('_id'=>1)));

count有效,因为它返回一个int可以添加到字符串中的内容。

编辑

离开:

$db->system->js->save(array(
    "_id" => "qo8qu0",
    "value" => new MongoCode("function() { return db.collection.find(filter).toArray() }")
));

然后在您的应用程序中:

$test = $db->execute("qo8qu0()", array('filter'=>array('_id'=>1)))
于 2013-07-28T00:41:02.783 回答