php - Get an average number for multiple embedded documents MongoDB -
i working highcharts make drilldown chart. display multiple projects, average value , drilldown in columns every value. problem is, can't average number per projects.. json mongo looks this:
{ "_id" : objectid("57f4dd6c45db428df9a5541b"), "id" : 68, "project_code" : 13110202, "samples" : [ { "sample_name" : "1678_2", "perc" : "75.97" }, { "sample_name" : "2012_1b-5", "perc" : "75.33", }, { "sample_name" : "2012_1b-8", "perc" : "77.28", }] }, { "_id" : objectid("57f4dd6c45db428df9a5541a"), "id" : 70, "project_code" : 1235125, "samples" : [ { "sample_name" : "ben", "perc" : "30.97" }, { "sample_name" : "anna", "perc" : "81.25", }, { "sample_name" : "sarah", "perc" : "80.23", }] },
i trying through document, through embedded document samples, perc_mapped values, calculate average , put in json used highchart. code have now:
foreach ($id_array $char) { $cursor = $collection->find(array("_id" => new mongoid($char))); foreach ($cursor $document) { if (is_array($document['samples'])) { foreach ($document['samples'] $samples) { array_push($perc_total, $samples['perc']); $average_of_foo = array_sum($perc_total) / count($perc_total); } } } } var_dump($average_of_foo); foreach ($id_array $char) { $cursor = $collection->find(array("_id" => new mongoid($char))); foreach ($cursor $document) { if (is_array($document['samples'])) { foreach ($document['samples'] $samples) { $array = array("name" => $document['project_code'], // here add average "y" => (float) $samples['perc'], "drilldown" => $char ); array_push($array_data, $array); } } } }
Comments
Post a Comment