printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__); exit; } try { $cursor = $collection->find(); echo "Find all docs, should be 3, verify 1x USA citizen, 2x Icelandic\n"; foreach ($cursor as $document) { var_dump($document); } echo "\n"; $result = $collection->distinct("citizen"); echo "Distinct countries:\n"; var_dump($result); echo "\n"; echo "aggregate\n"; $result = $collection->aggregate([['$project' => ["name" => 1, "_id" => 0]]], ["useCursor" => true, "batchSize" => 2]); printf("Should be 3 different people\n"); foreach ($result as $person) { var_dump($person); } echo "\n"; } catch (Exception $e) { printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__); exit; } try { $result = $collection->updateMany(["citizen" => "Iceland"], ['$set' => ["viking" => true]]); printf("Updated: %d (out of expected 2), verify Icelandic people are vikings\n", $result->getModifiedCount()); $result = $collection->find(); foreach ($result as $document) { var_dump($document);
$extra_fields = ['year' => ['$first' => '$year']]; $sort_field = "year"; $ord = 1; $filter = ["continent" => $parts[0], "country" => $parts[1]]; //This is our last drilldown level, so we won't set ID to retrieved values: $put_id = null; } } } // first build group section for aggregation query: $group = ['_id' => $group_field, 'count' => ['$sum' => 1], 'sum' => ['$sum' => '$value']]; if ($extra_fields) { $group = array_merge($group, $extra_fields); } // execute the aggregation query against the MongoDB database. $queryResult = $collection->aggregate([['$match' => (object) $filter], ['$group' => $group], ['$sort' => [$sort_field => $ord]], ['$limit' => $limit]]); //Now build the data in format that is supported by FacetChart $chartValues = []; $count = 0; //In this example we will make FacetChart to ask for subvalues on each bar clicked. foreach ($queryResult as $row) { $r = ["name" => $row->_id, "value" => $row->sum]; //Putting "ID" in chartValues, means that FacetChart will ask for subvalues. if ($put_id) { //Let's construct our own custom ID: $val_id = $id ? $id . "_" . $row->_id : $row->_id; $r["id"] = $val_id; } $chartValues[] = $r; $count++; }
$dateFormat = "%Y-%m-%dT%H:00:00.000Z"; break; case "d": $dateFormat = "%Y-%m-%dT00:00:00.000Z"; break; case "M": $dateFormat = "%Y-%m-01T00:00:00.000Z"; break; case "y": $dateFormat = "%Y-01-01T00:00:00.000Z"; break; default: throw new Exception("Incorrect 'unit' specified in the URL."); } // execute the aggregation query against the MongoDB database. $queryResult = $collection->aggregate([['$match' => (object) $filter], ['$sort' => [$timeField => 1]], ['$group' => ['_id' => ['$dateToString' => ['format' => $dateFormat, 'date' => '$' . $timeField]], 'first' => ['$first' => '$' . $valueField], 'last' => ['$last' => '$' . $valueField], 'min' => ['$min' => '$' . $valueField], 'max' => ['$max' => '$' . $valueField], 'count' => ['$sum' => 1], 'sum' => ['$sum' => '$' . $valueField]]], ['$sort' => ['_id' => 1]], ['$limit' => $limit]]); // $from and $to are used to inform the chart about the data range this response covers // while in many scenarios it matches the timestamps of the first and last value, they // might be different if it is known that there are empty time ranges either at the beginning // or the end. $from = isset($_GET["from"]) && $_GET["from"] ? (double) $_GET["from"] : null; $to = null; // now build the data in format that is supported by TimeChart $chartValues = []; $count = 0; foreach ($queryResult as $row) { // unfortunately PHP does not support retrieving timestamps with millisecond precision // so this workaround is used. $phpTime = DateTime::createFromFormat('Y-m-d\\TH:i:s.uO', $row->_id); $jsTime = (double) ($phpTime->getTimestamp() . substr($phpTime->format("u"), 0, 3)); $chartValues[] = [$jsTime, $row->min, $row->max, $row->first, $row->last, $row->sum, $row->count];
$parts = explode("_", $id); $c = count($parts); if ($c == 1) { $group_field = "country"; $filter = ["continent" => $id]; } else { if ($c == 2) { $group_field = "name"; $filter = ["continent" => $parts[0], "country" => $parts[1]]; //This is our last drilldown level, so we won't set ID to retrieved values: $put_id = null; } } } // execute the aggregation query against the MongoDB database. $queryResult = $collection->aggregate([['$match' => (object) $filter], ['$group' => ['_id' => "\$" . $group_field, 'count' => ['$sum' => 1], 'sum' => ['$sum' => '$value']]], ['$sort' => [$sort_field => -1]], ['$limit' => $limit]]); //Now build the data in format that is supported by PieChart $chartValues = []; $count = 0; //It is possible to add subvalues to each record, //but in this example we will make piechart to ask for subvalues on each slice click. $subvalues = null; foreach ($queryResult as $row) { $r = ["name" => $row->_id, "value" => $row->sum, "subvalues" => $subvalues]; //Putting "ID" in chartValues, means that PieChart will ask for subvalues. if ($put_id) { //Let's construct our own custom ID: $val_id = $id ? $id . "_" . $row->_id : $row->_id; $r["id"] = $val_id; } $chartValues[] = $r;