group

@param array $keys    keys
@param array $query   query
@param mixed $initial initial
@param mixed $reduce  reduce

@return mixed
349338
public static function group(array $keys, array $query, $initial = null, $reduce = null)
    {

        if (!$reduce) $reduce = new \MongoCode('function (doc, out) { out.object = doc }');
        if(!$initial) $initial = array('object'=>0);

        return self::connection()->group(self::collectionName(), $keys, $initial, $reduce, array('condition'=>$query));

    }
134838
public function reduce(callable $callback, $initial = null)
    {
        foreach ($this->generator as $key => $value) {
            $initial = $callback($initial, $value, $key);
        }

        return $initial;
    }
1363717
public static function groupElementsByValueKeys($array, $groupKeys) {

        if (!is_array($groupKeys)) {
            $groupKeys = array($groupKeys);
        }

        $newArray = new AssociativeArray();
        foreach ($array as $element) {

            $applyObject = $newArray;
            foreach ($groupKeys as $index => $groupKey) {
                if ($index < sizeof($groupKeys) - 1) {
                    $subArray = $applyObject[$element[$groupKeys[$index]]];
                    if (!$subArray) {
                        $subArray = new AssociativeArray();
                        $applyObject[$element[$groupKeys[$index]]] = $subArray;
                    }
                    $applyObject = $subArray;
                } else {
                    if (!is_array($applyObject[$element[$groupKeys[$index]]])) {
                        $applyObject[$element[$groupKeys[$index]]] = array();
                    }
                    $applyObject[$element[$groupKeys[$index]]][] = $element;

                }
            }

        }

        
        return $newArray->toArray();
    }
557919
public function reduce(callable $func, $initial = null)
    {
        $answer = new HaystackReduce($this->toArray());
        return $answer->reduce($func, $initial);
    }
1340129
public function setQueryKeys(array $keys)
    {
        $this->queryKeys = [];

        foreach ($keys as $key) {
            if (is_string($key)) {
                $this->addQueryKey($key);
            }
        }

        return $this;
    }
478643
public function finalize($finalize)
    {
        switch ($this->query['type']) {
            case Query::TYPE_MAP_REDUCE:
                $this->query['mapReduce']['options']['finalize'] = $finalize;
                break;

            case Query::TYPE_GROUP:
                $this->query['group']['options']['finalize'] = $finalize;
                break;

            default:
                throw new BadMethodCallException('mapReduce(), map() or group() must be called before finalize()');
        }

        return $this;
    }
754069
function reduce($key,$handler,$finalize=null){
		$this->_reduce[$key]=[$handler,$finalize];
	}
1075302
public function buildQueryString(array $parameters, array $initialQueryParameters = [], $previousKey = null)
    {
        $queryParameters = $initialQueryParameters;

        foreach ($parameters as $key => $value) {
            if (null !== $previousKey) {
                $key = "{$previousKey}[{$key}]";
            }

            if (is_array($value)) {
                $queryParameters = $this->buildQueryString($value, $queryParameters, $key);
            } else {
                $queryParameters[] = "{$key}={$value}";
            }
        }

        return null !== $previousKey ? $queryParameters : implode('&', $queryParameters);
    }
1269844
public function generateCacheKeys($query, $params, $types, array $connectionParams = [])
    {
        $realCacheKey = 'query=' . $query .
            '&params=' . serialize($params) .
            '&types=' . serialize($types) .
            '&connectionParams=' . hash('sha256', serialize($connectionParams));

        // should the key be automatically generated using the inputs or is the cache key set?
        if ($this->cacheKey === null) {
            $cacheKey = sha1($realCacheKey);
        } else {
            $cacheKey = $this->cacheKey;
        }

        return [$cacheKey, $realCacheKey];
    }
401635
public static function groupBy($data, $key)
    {
        $res = [];
        for ($i = 0, $l = sizeof($data); $i < $l; $i++) {
            $item = $data[$i];
            $value = empty($item[$key]) ? __undefined : $item[$key];
            if (empty($res[$value])) {
                $res[$value] = [];
            }
            $res[$value][] = $item;
        }
        return $res;
    }
87825
public static function load($query, $fields = null)
    {
        //
        static::applySchema();

        //
        if (is_array($query)) {
            return static::loadByQuery($query, $fields);
        }

        //
        $key = static::getPrimaryKey();

        //
        return $key
             ? static::loadByPrimaryKey($query, $fields)
             : static::loadByMainField($query, $fields);
    }
1187477
public function delete_many(array $keys) {
        // The number of items that have actually being removed.
        $reduction = 0;
        foreach ($keys as $key) {
            if (isset($this->store[$key])) {
                $reduction++;
            }
            unset($this->store[$key]);
        }
        if ($this->maxsize !== false) {
            $this->storecount -= $reduction;
        }
        return $reduction;
    }
1121750
public function reduce(...$keys)
    {
        $keys = $this->normalizeKeys($keys);

        foreach ($this->keys() as $key) {
            if (!in_array($key, $keys, true)) {
                $this->remove($key);
            }
        }

        return $this;
    }
960862
protected function reduceResult($result)
    {
        if (is_array($result) && (count($result) == 1)) {
            reset($result);
            return $this->reduceResult($result[key($result)]);
        } else {
            return $result;
        }
    }
576950
public function filterByPrimaryKeys($keys)
    {
        if (empty($keys)) {
            return $this->add(null, '1<>1', Criteria::CUSTOM);
        }
        foreach ($keys as $key) {
            $cton0 = $this->getNewCriterion(SkillGroupTableMap::COL_SKILL_ID, $key[0], Criteria::EQUAL);
            $cton1 = $this->getNewCriterion(SkillGroupTableMap::COL_GROUP_ID, $key[1], Criteria::EQUAL);
            $cton0->addAnd($cton1);
            $this->addOr($cton0);
        }

        return $this;
    }
1228383
public ViewQuery reduce(final boolean reduce) {
        params[PARAM_REDUCE_OFFSET] = "reduce";
        params[PARAM_REDUCE_OFFSET+1] = Boolean.toString(reduce);
        return this;
    }
221723
public function withQueryParams(array $query)
    {
        $params = $this->getQueryParams();

        foreach ($query as $key => $value) {
            $params[$key] = $value;
        }

        $this->queryParams = $params;
    }