在 MongoDB 中使用群组

Using Group in MongoDB

我的数据库名称 "nutch" 中有一个集合 "crawl_data"。在 crawl_data 中,我有一个名为 "Domain" 的字段。现在我想计算与 PHP.

相同域名下的行数

MYSQL 等价物是:

 SELECT DOMAIN, COUNT(*) AS NUMOFURLS FROM Crawl_data GROUP BY DOMAIN

我试过代码:

$keys = array("Domain" => 1);
$inital = array("count" => 0);
$reduce = "function (obj, prev) { prev.count++; }";
$cursor = $collection->group($keys,$inital,$reduce);
foreach($cursor as $doc){
  echo var_dump($doc);
}

输出为:

  array(3) { [0]=> array(2) { ["Domain"]=> string(13) "Straits Times"      ["count"]=> float(5127) } 

  [1]=> array(2) { ["Domain"]=> string(7) "Reuters" ["count"]=> float(3201) } 

  [2]=> array(2) { ["Domain"]=> string(17) "Channel News Asia" ["count"]=> float(2812) } } float(11140) int(3) float(1)

print_r($curosr) 给出:

 Array ( [retval] => Array ( [0] => Array ( [Domain] => Straits Times   [count] => 5127 ) 
 [1] => Array ( [Domain] => Reuters [count] => 3201 ) 
 [2] => Array ( [Domain] => Channel News Asia [count] => 2812 ) ) [count] => 11140 [keys] => 3 [ok] => 1 ) 1

我应该如何输出 $doc 变量以仅回显域名和 计数

例如: 域名:海峡时报;人数:5127

foreach($cursor['retval'] as  $doc){

 $test .= $doc["Domain"] ." ".$doc["count"]."<br>";

}

echo $test;

您不应该在 MongoCollection 上使用 PHP 组函数 ( http://php.net/manual/en/mongocollection.group.php ) 来执行此操作。

这是一种非常古老且过时的分组方法。

现在您使用聚合框架 (http://php.net/manual/en/mongocollection.aggregate.php and http://docs.mongodb.org/manual/core/aggregation-introduction/):

$result = $db->collectionp->aggregate([
    ['$group' => ['_id' => '$Domain', 'count' => ['$sum' => 1]]]
]);

foreach($result['result'] as $doc){
    echo 'Domain: ' . $doc['_id'] . ' with count: ' . $doc['count'];
}