PHP » GoLang |
login |
register |
about
|
GoLang MongoCollection::aggregate
request it (412)
GoLang replacement for PHP's MongoCollection::aggregate
[edit | history]
PHP MongoCollection::aggregatePHP original manual for MongoCollection::aggregate [ show | php.net ]MongoCollection::aggregate(PECL mongo >=1.3.0) MongoCollection::aggregate — Perform an aggregation using the aggregation framework Description
public array MongoCollection::aggregate
( array
$pipeline
[, array $options
] )
public array MongoCollection::aggregate
( array
$op
[, array $op
[, array $...
]] )The MongoDB » aggregation framework provides a means to calculate aggregated values without having to use MapReduce. While MapReduce is powerful, it is often more difficult than necessary for many simple aggregation tasks, such as totaling or averaging field values. This method accepts either a variable amount of pipeline operators, or a single array of operators constituting the pipeline. Parameters
Or
Return ValuesThe result of the aggregation as an array. The ok will be set to 1 on success, 0 on failure. Errors/ExceptionsWhen an error occurs an array with the following keys will be returned:
Changelog
ExamplesExample #1 MongoCollection::aggregate() example The following example aggregation operation pivots data to create a set of author names grouped by tags applied to an article. Call the aggregation framework by issuing the following command:
<?php The above example will output: array(2) { ["result"]=> array(2) { [0]=> array(2) { ["_id"]=> array(1) { ["tags"]=> string(4) "good" } ["authors"]=> array(1) { [0]=> string(3) "bob" } } [1]=> array(2) { ["_id"]=> array(1) { ["tags"]=> string(3) "fun" } ["authors"]=> array(1) { [0]=> string(3) "bob" } } } ["ok"]=> float(1) } The following examples use the » zipcode data set. Use mongoimport to load this data set into your mongod instance. Example #2 MongoCollection::aggregate() example To return all states with a population greater than 10 million, use the following aggregation operation:
<?php The above example will output something similar to: array(2) { ["result"]=> array(7) { [0]=> array(2) { ["_id"]=> string(2) "TX" ["totalPop"]=> int(16986510) } [1]=> array(2) { ["_id"]=> string(2) "PA" ["totalPop"]=> int(11881643) } [2]=> array(2) { ["_id"]=> string(2) "NY" ["totalPop"]=> int(17990455) } [3]=> array(2) { ["_id"]=> string(2) "IL" ["totalPop"]=> int(11430602) } [4]=> array(2) { ["_id"]=> string(2) "CA" ["totalPop"]=> int(29760021) } [5]=> array(2) { ["_id"]=> string(2) "OH" ["totalPop"]=> int(10847115) } [6]=> array(2) { ["_id"]=> string(2) "FL" ["totalPop"]=> int(12937926) } } ["ok"]=> float(1) } Example #3 MongoCollection::aggregate() example To return the average populations for cities in each state, use the following aggregation operation:
<?php The above example will output something similar to: array(2) { ["result"]=> array(51) { [0]=> array(2) { ["_id"]=> string(2) "DC" ["avgCityPop"]=> float(303450) } [1]=> array(2) { ["_id"]=> string(2) "DE" ["avgCityPop"]=> float(14481.913043478) } ... [49]=> array(2) { ["_id"]=> string(2) "WI" ["avgCityPop"]=> float(7323.0074850299) } [50]=> array(2) { ["_id"]=> string(2) "WV" ["avgCityPop"]=> float(2759.1953846154) } } ["ok"]=> float(1) } Example #4 MongoCollection::aggregate() with command options To return information on how the pipeline will be processed we use the explain command option:
<?php The above example will output something similar to: array(2) { ["stages"]=> array(4) { [0]=> array(1) { ["$cursor"]=> array(3) { ["query"]=> array(0) { } ["fields"]=> array(3) { ["pop"]=> int(1) ["state"]=> int(1) ["_id"]=> int(0) } ["plan"]=> array(4) { ["cursor"]=> string(11) "BasicCursor" ["isMultiKey"]=> bool(false) ["scanAndOrder"]=> bool(false) ["allPlans"]=> array(1) { [0]=> array(3) { ["cursor"]=> string(11) "BasicCursor" ["isMultiKey"]=> bool(false) ["scanAndOrder"]=> bool(false) } } } } } [1]=> array(1) { ["$group"]=> array(2) { ["_id"]=> string(6) "$state" ["totalPop"]=> array(1) { ["$sum"]=> string(4) "$pop" } } } [2]=> array(1) { ["$match"]=> array(1) { ["totalPop"]=> array(1) { ["$gte"]=> int(10000000) } } } [3]=> array(1) { ["$sort"]=> array(1) { ["sortKey"]=> array(1) { ["totalPop"]=> int(-1) } } } } ["ok"]=> float(1) } See Also
|
more
Recently updated
more
Most requested
more
Last requests
|