我试图在php中使用mongodb-ODM运行这个确切的查询
db.runCommand ( { distinct: "messages",
key: "conversation",
query: { conversation: { $in: ["533f28c9211b6f7e448b4567","52cb29b0211b6fd9248b456b"] } }
} )
如何用distinct()转换它? 感谢。
答案 0 :(得分:0)
您可以看到我的实施:
定义在mongoDB中执行javascript查询的方法
/**
* 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