2013-05-04 67 views
0

我一直在嘗試使用蒙戈::命令在PHP構建的MapReduce但每次我的代碼運行時我得到以下錯誤:PHP Fatal Error, call to undefined method "mongo:command"PHP蒙戈:找不到命令

我的代碼:

try { 
    $map = new MongoCode("function() { 
        if (!this.tags) { 
         return; 
        } 

        for (index in this.tags) { 
         emit(this.tags[index], 1); 
        }"); 
    $reduce = new MongoCode("function(previous, current) { 
           var count = 0; 

           for (index in current) { 
            count += current[index]; 
           } 

           return count; 
          }"); 
    $tags = $this->db->command(array(  //Line the error is found on 
      "mapreduce" => "blog", 
      "map" => $map, 
      "reduce" => $reduce)); 
    $con=$this->db->selectCollection($tags['result'])->find(); 
    var_dump($con); 
} 
catch(MongoCursorException $e) { 
    echo "error message: ".$e->getMessage()."\n"; 
    echo "error code: ".$e->getCode()."\n"; 
} 

請注意$this->db是指我的連接(以前定義)和blog是集合。

僅供參考我用:http://php.net/manual/en/mongodb.command.php

我使用的操作系統是Ubuntu的12.04,我仔細檢查過這兩個文件的php.ini其中既包括mongo.so - 我可以做正常的查詢,有喜歡的MongoDB插入和獲取數據,它只是命令似乎不起作用。

回答

0

你喜歡選擇集合$d = $m->demo;

php.net:

<?php 
$m = new MongoClient(); 
$d = $m->demo; 
$c = $d->poiConcat; 

$r = $d->command(array(
'geoNear' => "poiConcat",  // Search in the poiConcat collection 
'near' => array(-0.08, 51.48), // Search near 51.48°N, 0.08°E 
'spherical' => true,   // Enable spherical search 
'num' => 5,     // Maximum 5 returned documents 
)); 
print_r($r); 
?> 

我想在你的代碼中沒有選擇集合$d = $this->db->demo;放集合名稱,而不是demo

try { 
    $map = new MongoCode("function() { 
       if (!this.tags) { 
        return; 
       } 

       for (index in this.tags) { 
        emit(this.tags[index], 1); 
       }"); 
    $reduce = new MongoCode("function(previous, current) { 
          var count = 0; 

          for (index in current) { 
           count += current[index]; 
          } 

          return count; 
         }"); 
    $d = $this->db->demo;// attention 
    $tags = $d->command(array(  //Line the error is found on 
     "mapreduce" => "blog", 
     "map" => $map, 
     "reduce" => $reduce)); 
    $con=$d->selectCollection($tags['result'])->find(); 
    var_dump($con); 
} 
catch(MongoCursorException $e) { 
    echo "error message: ".$e->getMessage()."\n"; 
    echo "error code: ".$e->getCode()."\n"; 
} 

編輯樣品:我做這個樣品看它

try { 
    $map = new MongoCode("function() { emit(this.user_id,1); }"); 
    $reduce = new MongoCode("function(k, vals) { ". 
     "var sum = 0;". 
     "for (var i in vals) {". 
     "sum += vals[i];". 
     "}". 
     "return sum; }"); 
     $db=new Mongo("mongodb://sepidar:[email protected]:27017/admin");     
     $d = $db->SepidarSoft_CBMS;// attention 
     $tags = $d->command(array(  //Line the error is found on 
      'mapReduce'=>'RunUser', 
      "map" => $map, 
      "reduce" => $reduce, 
      "out" => array('merge'=>'SepidarSoft_CBMS', 'db'=> 'RunUser') 
    )); 
     print_r($tags); 
} 
catch(MongoCursorException $e) { 
    echo "error message: ".$e->getMessage()."\n"; 
    echo "error code: ".$e->getCode()."\n"; 
} 

my mongodb struct

結果

Array 
(
    [result] => Array 
    (
     [db] => RunUser 
     [collection] => SepidarSoft_CBMS 
    ) 

    [timeMillis] => 2 
    [counts] => Array 
    (
     [input] => 1 
     [emit] => 1 
     [reduce] => 0 
     [output] => 1 
    ) 

    [ok] => 1 
) 
+0

的分貝被選擇時,限定'$這 - > db'時。正如前面提到的,它適用於'$ this-> db-> blog-> find();' – Daniel 2013-05-04 15:20:52

+0

你測試用'blog'替換'demo' – 2013-05-04 15:44:50

+0

是的,我試過了,但是我得到了同樣的錯誤。我也許應該說'blog'不是數據庫,它是一個集合作爲數據庫的一部分。 – Daniel 2013-05-04 21:38:52