downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

MongoCollection::insert> <MongoCollection::getName
Last updated: Fri, 27 Nov 2009

view this page in

MongoCollection::group

(PECL mongo >=0.9.2)

MongoCollection::groupPerforms an operation similar to SQL's GROUP BY command

Description

public array MongoCollection::group ( array $keys , array $initial , string $reduce [, array $condition = array() ] )

Parameters

keys

Fields to group by.

initial

Initial value of the aggregation counter object.

reduce

A function that aggregates (reduces) the objects iterated.

condition

An condition that must be true for a row to be considered.

Return Values

Returns an array containing the result.

Examples

Example #1 MongoCollection::group() example

<?php

$collection
->save(array("a" => 2));
$collection->save(array("b" => 5));
$collection->save(array("a" => 1));

// use all fields
$keys = array();

// set intial values
$initial = array("count" => 0);

// JavaScript function to perform
$reduce "function (obj, prev) { prev.count++; }";

// only use documents where the "a" field is greater than 1
$condition = array("a" => array( '$gt' => 1));

$g $collection->group($keys$initial$reduce$condition);

var_dump($g);

?>

The above example will output something similar to:

array(4) {
  ["retval"]=>
  array(1) {
    [0]=>
    array(1) {
      ["count"]=>
      float(1)
    }
  }
  ["count"]=>
  float(1)
  ["keys"]=>
  int(1)
  ["ok"]=>
  float(1)
}


add a note add a note User Contributed Notes
MongoCollection::group
There are no user contributed notes for this page.

MongoCollection::insert> <MongoCollection::getName
Last updated: Fri, 27 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites