2014-09-02 105 views
0

我試圖用MongoDB的-ODM運行PHP這個確切的查詢轉換控制檯命令ODM

db.runCommand ({ distinct: "messages", 
        key: "conversation", 
        query: { conversation: { $in: ["533f28c9211b6f7e448b4567","52cb29b0211b6fd9248b456b"] } } 
       }) 

我如何與不同的transate它()? 謝謝。

+0

我會從閱讀您的ODM文檔開始,以其爲準。很高興知道你正在使用的確切庫以及你已經寫過的任何代碼。 – Roberto 2014-09-03 02:51:30

回答

0

你可以看到我的實現:

定義這個方法,在MongoDB中

/** 
    * 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, 
      )); 
    } 

執行JavaScript的查詢定義您的查詢字符串

$_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