你可以看到我的实现:
定义这个在 mongoDB 中执行 javascript Query 的方法
/**
* Execute javascript in mongodb
*
* @param string $js JavaScript function
*
* @return array
*/
protected function executeJs($js)
{
// get database name
$mongoDatabaseName = $this->dm->getConfiguration()->getDefaultDB();
// get connection
$m = $this->dm->getConnection();
// return results, get mongodb client
return $m->getMongo()
// select database
->selectDB($mongoDatabaseName)
// execute javasctipt function
->command(array(
// js
'eval' => $js,
// no lock database, while js will be executed
'nolock' => true,
));
}
定义你的字符串查询
$_myQuery = 'function() {var messages = [];
db.runCommand ( { distinct: "messages",
key: "conversation",
query: { conversation: { $in: ["533f28c9211b6f7e448b4567","52cb29b0211b6fd9248b456b"] } }
}
).forEach(function(msg){ messages[]= msg });
return messages; }';
最后,执行您的查询
$messageCollection = $this->executeJs($_myQuery);
if(isset(messageCollection))
var_dump(messageCollection['retval']); // it will show result