MongoCollection::group

(PECL mongo >=0.9.2)

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

说明

public MongoCollection::group ( mixed $keys , array $initial , MongoCode $reduce [, array $options = array() ] ) : array

参数

keys

Fields to group by. If an array or non-code object is passed, it will be the key used to group results.

1.0.4+: If keys is an instance of MongoCode, keys will be treated as a function that returns the key to group by (see the "Passing a keys function" example below).

initial

Initial value of the aggregation counter object.

reduce

A function that takes two arguments (the current document and the aggregation to this point) and does the aggregation.

options

Optional parameters to the group command. Valid options include:

  • "condition"

    Criteria for including a document in the aggregation.

  • "finalize"

    Function called once per unique key that takes the final output of the reduce function.

  • "maxTimeMS"

    Specifies a cumulative time limit in milliseconds for processing the operation on the server (does not include idle time). If the operation is not completed by the server within the timeout period, a MongoExecutionTimeoutException will be thrown.

返回值

Returns an array containing the result.

更新日志

版本 说明
1.5.0 Added "maxTimeMS" option.
1.2.11 Emits E_DEPRECATED when options is scalar.

范例

Example #1 MongoCollection::group() example

This groups documents by category and creates a list of names within that category.

<?php

$collection
->insert(array("category" => "fruit""name" => "apple"));
$collection->insert(array("category" => "fruit""name" => "peach"));
$collection->insert(array("category" => "fruit""name" => "banana"));
$collection->insert(array("category" => "veggie""name" => "corn"));
$collection->insert(array("category" => "veggie""name" => "broccoli"));

$keys = array("category" => 1);

$initial = array("items" => array());

$reduce "function (obj, prev) { prev.items.push(obj.name); }";

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

echo 
json_encode($g['retval']);

?>

以上例程的输出类似于:

[{"category":"fruit","items":["apple","peach","banana"]},{"category":"veggie","items":["corn","broccoli"]}]

Example #2 MongoCollection::group() example

This example doesn't use any key, so every document will be its own group. It also uses a condition: only documents that match this condition will be processed by the grouping function.

<?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('condition' => array("a" => array( '$gt' => 1)));

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

var_dump($g);

?>

以上例程的输出类似于:

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

Example #3 Passing a keys function

If you want to group by something other than a field name, you can pass a function as the first parameter of MongoCollection::group() and it will be run against each document. The return value of the function will be used as its grouping value.

This example demonstrates grouping by the num field modulo 4.

<?php

$c
->group(new MongoCode('function(doc) { return {mod : doc.num % 4}; }'),
     array(
"count" => 0),
     new 
MongoCode('function(current, total) { total.count++; }'));

?>
相关文章
php domdocument performs relaxng validation on the documentphp ldap 函数 performs an extended operationphp mssql 函数 select ms sql databasephp postgresql 函数 submits a command to the server and waits for the result with the ability to pass parameters separately from the sql command textphp posix 函数 return info about a group by group idphp com 函数 performs a bitwise and operation between two variantsphp mongocollection execute an aggregation pipeline command and retrieve results through a cursorphp mongocollection queries this collection returning a single elementphp mongocollection performs an operation similar to sql s group by commandphp mongocollection converts keys specifying an index to its identifying stringphp mongodb driver monitoring commandfailedevent returns the command s operation idphp mongodb driver monitoring commandstartedevent returns the command s operation idphp mongodb driver monitoring commandsubscriber notification method for a successful commandphp mongodb driver monitoring commandsucceededevent returns the command s operation idphp mongodb checks for the last error thrown during a database operationphp mysqli stmt get the id generated from the previous insert operationphp norewinditerator prevents the rewind operation on the inner iteratorphp solrquery enable/disable result grouping group parameter php solrquery if true the result of the first field grouping command is used as the main result list in the response using group.format=simplephp sqlite3 registers a php function for use as an sql scalar function
关注编程学问公众号