Reputation: 655
I have a collection "crawl_data" in my DB name "nutch". In crawl_data, I have a field called "Domain". Now I want to count the number of rows that are under the same Domain name with PHP.
The MYSQL equivalent would be:
SELECT DOMAIN, COUNT(*) AS NUMOFURLS FROM Crawl_data GROUP BY DOMAIN
I tried the code :
$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);
}
The output is :
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) gives:
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
How should I output the $doc variable to echo only the domain name and the count number
For example: Domain :Straits Times; number : 5127
Upvotes: 0
Views: 44
Reputation: 43884
You shouldn't be using the PHP group function ( http://php.net/manual/en/mongocollection.group.php ) on the MongoCollection
to do this.
That is a very old and out dated method of grouping.
Now-a-days you use the aggregation framework ( 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'];
}
Upvotes: 1
Reputation: 150
foreach($cursor['retval'] as $doc){
$test .= $doc["Domain"] ." ".$doc["count"]."<br>";
}
echo $test;
Upvotes: 1