group() public méthode

Performs an operation similar to SQL's GROUP BY command
public group ( mixed $keys, array $initial, MongoCode | string $reduce, array $condition = [] ) : array
$keys mixed Fields to group by. If an array or non-code object is passed, it will be the key used to group results.
$initial array Initial value of the aggregation counter object.
$reduce MongoCode | string A function that aggregates (reduces) the objects iterated.
$condition array An condition that must be true for a row to be considered.
Résultat array
 public function testGroup2()
 {
     $this->object->save(array("a" => 2));
     $this->object->save(array("b" => 5));
     $this->object->save(array("a" => 1));
     $keys = array();
     $initial = array("count" => 0);
     $reduce = "function (obj, prev) { prev.count++; }";
     $g = $this->object->group($keys, $initial, $reduce);
     $this->assertEquals(3, $g['count']);
 }
 public function testGroupFinalize2()
 {
     for ($i = 0; $i < 100; $i++) {
         $this->object->insert(array("x" => $i, "y" => $i % 7, "z" => "foo{$i}"));
     }
     $result = $this->object->group(array("y" => 1), array("count" => 0), "function(cur, prev) { prev.count++; }", array("finalize" => "function(out) { return 1; }"));
     $this->assertEquals(true, (bool) $result['ok'], json_encode($result));
     foreach ($result['retval'] as $val) {
         $this->assertEquals(1, $val);
     }
     $this->assertEquals(100, $result['count']);
     $this->assertEquals(7, $result['keys']);
 }
 public function testGroup()
 {
     $g = $this->object->group(array(), array("count" => 0), "function (obj, prev) { prev.count++; }", array());
     $this->assertEquals(0, count($g['retval']));
     $this->object->save(array("a" => 2));
     $this->object->save(array("b" => 5));
     $this->object->save(array("a" => 1));
     $g = $this->object->group(array(), array("count" => 0), "function (obj, prev) { prev.count++; }", array());
     $this->assertEquals(1, count($g['retval']));
     $this->assertEquals(3, $g['retval'][0]['count']);
     $g = $this->object->group(array(), array("count" => 0), "function (obj, prev) { prev.count++; }", array("a" => array('$gt' => 1)));
     $this->assertEquals(1, count($g['retval']));
     $this->assertEquals(1, $g['retval'][0]['count']);
 }
Exemple #4
0
 /**
  * group.
  */
 public function group($keys, $initial, $reduce, array $options = array())
 {
     $this->time->start();
     $return = parent::group($keys, $initial, $reduce, $options);
     $time = $this->time->stop();
     $this->log(array('type' => 'group', 'keys' => $keys, 'initial' => $initial, 'reduce' => $reduce, 'options' => $options, 'time' => $time));
     return $return;
 }
Exemple #5
0
 protected function _call($command, array $arguments = array(), array $values = NULL)
 {
     $start = microtime(true);
     $this->_connected or $this->connect();
     extract($arguments);
     if (isset($collection_name)) {
         $c = new \MongoCollection($this->_db, $collection_name);
     }
     switch ($command) {
         case 'ensure_index':
             $r = $c->ensureIndex($keys, $options);
             break;
         case 'create_collection':
             $r = $this->_db->createCollection($name, $capped, $size, $max);
             break;
         case 'drop_collection':
             $r = $this->_db->dropCollection($name);
             break;
         case 'command':
             $r = $this->_db->command($values);
             break;
         case 'execute':
             $r = $this->_db->execute($code, $args);
             break;
         case 'batch_insert':
             $r = $c->batchInsert($values);
             break;
         case 'count':
             $r = $c->count($query);
             break;
         case 'find_one':
             $r = $c->findOne($query, $fields);
             break;
         case 'find':
             $r = $c->find($query, $fields);
             break;
         case 'group':
             $r = $c->group($keys, $initial, $reduce, $condition);
             break;
         case 'update':
             $r = $c->update($criteria, $values, $options);
             break;
         case 'insert':
             $r = $c->insert($values, $options);
             return $values;
             break;
         case 'remove':
             $r = $c->remove($criteria, $options);
             break;
         case 'save':
             $r = $c->save($values, $options);
             break;
         case 'get_file':
             $r = $this->gridFS()->findOne($criteria);
             break;
         case 'get_files':
             $r = $this->gridFS()->find($query, $fields);
             break;
         case 'set_file_bytes':
             $r = $this->gridFS()->storeBytes($bytes, $extra, $options);
             break;
         case 'set_file':
             $r = $this->gridFS()->storeFile($filename, $extra, $options);
             break;
         case 'remove_file':
             $r = $this->gridFS()->remove($criteria, $options);
             break;
     }
     $this->log($command, $start, $arguments);
     return $r;
 }
Exemple #6
0
 protected function doGroup($keys, array $initial, $reduce, array $options)
 {
     $result = $this->mongoCollection->group($keys, $initial, $reduce, $options);
     return new ArrayIterator($result);
 }