我在rockmongo工具中运行了此命令
及其返回的正确值
{
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中时,它不能正常工作.
我正在使用使用PECL类库的phalcon框架来访问mongodb
和rockmongo作为访问mongodb集合的GUI
它为find()count()等返回正确的值.是否应该将另一个库用于聚合或管道?
解决方法:
集合应该是一个数组数组.我想在这里,您错过了一个数组.
该文件说:
$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);
标签:mongodb,php
来源: https://codeday.me/bug/20191119/2035253.html