【问题标题】:PHP Mongo:command not foundPHP Mongo:找不到命令
【发布时间】:2013-05-04 14:43:12
【问题描述】:

我一直在尝试使用 PHP 中的 mongo::command 来构建 MapReduce,但每次运行代码时都会出现以下错误:PHP Fatal Error, call to undefined method "mongo:command"

我的代码:

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,我已经仔细检查了两个包含 mongo.so 的 php.ini 文件 - 我可以使用 mongodb 进行正常查询,例如插入和获取数据,只是命令似乎不起作用。

【问题讨论】:

    标签: php mongodb-php


    【解决方案1】:

    你选择像$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-&gt;db-&gt;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: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";
    }
    

    结果

     Array
    (
        [result] => Array
        (
            [db] => RunUser
            [collection] => SepidarSoft_CBMS
        )
    
        [timeMillis] => 2
        [counts] => Array
        (
            [input] => 1
            [emit] => 1
            [reduce] => 0
            [output] => 1
        )
    
        [ok] => 1
    )
    

    【讨论】:

    • 在定义 $this-&gt;db 时选择了 db。如前所述,它适用于$this-&gt;db-&gt;blog-&gt;find();
    • 你用blog替换demo进行测试
    • 是的,我试过了,但我得到了同样的错误。我或许应该说blog 不是数据库,它是作为数据库一部分的集合。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 2021-12-15
    • 2019-04-16
    • 2017-09-18
    • 1970-01-01
    • 2019-06-17
    相关资源
    最近更新 更多