2013-05-04 2 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"; 
} 

Редактировать образец: i Сделайте этот образец, см. Его

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

Выбрана дБ, при определении '$ this-> db'. Как упоминалось, он работает для '$ this-> db-> blog-> find();' – Daniel

+0

вы проверяете с заменой 'demo' на' blog' –

+0

Да, я пробовал это, но получаю ту же ошибку. Возможно, я должен сказать, что 'blog' - это не база данных, это коллекция как часть базы данных. – Daniel