In this example, we are going to calculate the average rating each author received for his or her articles—published within the last 24 hours—using the
group() method. We are going to execute this method within a PHP program. The program will output a HTML table, displaying the total number of articles and average rating for each author:
avg_rating.php. Put the following code in it:
<?php require('dbconnection.php'); $mongo = DBConnection::instantiate(); $collection = $mongo->getCollection('sample_articles'); $key = array('author' => 1); //set both the aggregation counter and total rating to zero $initial = array('count' => 0, ...