diff --git a/reference/mongo/mongocollection/aggregate.xml b/reference/mongo/mongocollection/aggregate.xml new file mode 100644 index 0000000000..17be842531 --- /dev/null +++ b/reference/mongo/mongocollection/aggregate.xml @@ -0,0 +1,186 @@ + + + + + + MongoCollection::aggregate + Perform an aggregation using the aggregation framework + + + + &reftitle.description; + + public arrayMongoCollection::aggregate + arraypipelines + arraypipeline + array.. + + + The MongoDB aggregation framework provides a means to calculate aggregated + values without having to use map-reduce. While map-reduce is powerful, it is + often more difficult than necessary for many simple aggregation tasks, such + as totaling or averaging field values. + + + This method accepts both variable amount of + pipelines, or an array of pipelines. + + + + + &reftitle.parameters; + + + pipelines + + + An array of >pipelines, or just the first part. + + + + + pipeline + + + Second array of pipes. + + + + + .. + + + More pippes... + + + + + + + + &reftitle.returnvalues; + + The result of the aggregation as an array. + + + + + &reftitle.examples; + + <methodname>MongoCollection::aggregate</methodname> 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: + + +selectDB("examples")->selectCollection("article"); +$c->drop(); +$data = array ( + 'title' => 'this is my title', + 'author' => 'bob', + 'posted' => new MongoDate, + 'pageViews' => 5, + 'tags' => array ( 'fun', 'good', 'fun' ), + 'comments' => array ( + array ( + 'author' => 'joe', + 'text' => 'this is cool', + ), + array ( + 'author' => 'sam', + 'text' => 'this is bad', + ), + ), + 'other' =>array ( + 'foo' => 5, + ), +); +$d = $c->insert($data, array("safe" => true)); + +$ops = array( + array( + '$project' => array( + "author" => 1, + "tags" => 1, + ) + ), + array('$unwind' => '$tags'), + array( + '$group' => array( + "_id" => array("tags" => 1), + "authors" => array('$addToSet' => '$author'), + ), + ), +); +$results = $c->aggregate($ops); +var_dump($results); +]]> + + &example.outputs; + + + 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" + } + } +} +]]> + + + + + + &reftitle.seealso; + + The MongoDB aggregation framework + + + + + + diff --git a/reference/mongo/versions.xml b/reference/mongo/versions.xml index 3ca9359c1f..cbb5e43265 100644 --- a/reference/mongo/versions.xml +++ b/reference/mongo/versions.xml @@ -56,6 +56,7 @@ +