简体   繁体   中英

PHP Mongo:command not found

I have been trying to use the mongo::command in PHP to build a MapReduce but every time I run my code I get the following error: PHP Fatal Error, call to undefined method "mongo:command"

My Code:

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";
}

Please note $this->db refers to my connection (previously defined) and blog is the collection.

For reference I have used: http://php.net/manual/en/mongodb.command.php

The OS I use is Ubuntu 12.04 and I've double checked both php.ini files which both include mongo.so - I can do normal queries with mongodb like inserting and fetching data, its just the command seems not to work.

do you select collection like $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);
?>

i think in your code you didn't select collection $d = $this->db->demo; put collection name instead of 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";
}

Edit Sample:i Do this sample see it

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:123@localhost: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";
}

我的mongodb结构

result

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

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

    [ok] => 1
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM