在关系型数据库的Doctrine中,我们有QueryBuilder可以手动编写DQL查询,如果必要的话,可以使用Doctrine的连接执行原始SQL。但是我在Doctrine的Mongo项目中没有找到(无论在API还是文档中)如何执行这个操作的方法。除了注入doctrine_mongodb.odm.default_connection之外,我应该如何使用mongo odm执行本机查询呢?
private function _getNativeConnection(){
$connection = $this->getDocumentManager()->getConnection();
$mongo = $connection->getMongo();
if(!$mongo){
$connection->connect();
$mongo = $connection->getMongo();
}
//You can use this as literal strings, or pass them as parameters to the method
$db = $mongo->selectDB('YOUR_MONGO_DB')->selectCollection("MONGO_COLLECTION");
return $db;
}
public function another_public_method{
...
$collection = $this->_getNativeConnection();
...
}
$collection = $documentManager->getDocumentCollection('Vendor\MyDocument');
然后你可以做这样的事情
$collection->aggregate($pipeline);