在mongodb中的聚合查询在php中返回null

时间:2015-12-11 09:00:18

标签: php mongodb

我在rockmongo tools-命令中运行了这个命令 并返回正确的值

{ 
    aggregate : "twits",                                
    pipeline : [
         {$group : {_id : "$by", num_tutorial : {$sum : 1}}}
    ]                
}

然后我尝试使用此

从中提取值
 define("__COLLECTION_TWITS__","twits");
/var/www/html/TradeTwits/require/config/DBConfig.inc.php

中的

$collectionName=__COLLECTION_TWITS__;
$db = $this->connection("mo");
$col = $db->$collectionName;

return $col->aggregate(['$group'=> array('_id' => '$by','num_tutorial' => array($sum => 1))]); 
/var/www/html/TradeTwits/app/models/BaseModel.php

中的

上面的代码应该返回按用户名分组的所有记录, 但是在php中,它运行不正常。

使用plc框架使用PECL类库访问mongodb 和rockmongo作为访问mongodb集合的GUI

它返回find() count()等的正确值。我应该使用其他库进行聚合还是管道?

1 个答案:

答案 0 :(得分:3)

Aggregate应该是一个数组数组。我想在这里,你错过了一个阵列。

文档说:

$ops = array(
    array(
        '$project' => array(
            "author" => 1,
            "tags"   => 1,
        )
    ),
    array('$unwind' => '$tags'),
    array(
        '$group' => array(
            "_id" => array("tags" => '$tags'),
            "authors" => array('$addToSet' => '$author'),
        ),
    ),
);
$results = $c->aggregate($ops);
相关问题