group

@param string $collection_name collection name
@param array  $keys            keys
@param array  $initial         initial
@param array  $reduce          reduce
@param array  $condition       condition

@return mixed
14930
public function group ($collection_name, $keys , array $initial , $reduce, array $condition = array())
    {
        return $this->_call(
            'group', array(
                'collection_name' => $collection_name,
                'keys'            => $keys,
                'initial'         => $initial,
                'reduce'          => $reduce,
                'condition'       => $condition
            )
        );
    }
407559
function _groupBy(collection, getter) {
        var result = {};
        var prop;

        forEach( collection, function( elm ) {
          prop = getter(elm);

          if(!result[prop]) {
            result[prop] = [];
          }
          result[prop].push(elm);
        });
        return result;
      }
528928
public function count($collectionName, $condition = [], $options = [])
    {
        $document = ['count' => $collectionName];

        if (!empty($condition)) {
            $document['query'] = (object) $this->buildCondition($condition);
        }

        return array_merge($document, $options);
    }
1346639
public static function toKeyIndex($collection, $key, $useDotSyntax = false)
    {
        $keyIndexArray = [];
        foreach ($collection as $item) {
            $itemArray = is_array($item) ? $item : (method_exists($item, 'toArray') ? $item->toArray() : []);
            $keyValue = self::get($itemArray, $key, null, $useDotSyntax);
            $keyIndexArray[$keyValue] = $itemArray;
        }

        return $keyIndexArray;
    }
1053687
function reduce(arr, iter, base) {
    arr = Immutable.Iterable.isIterable(arr)? arr : Immutable.List(arr);

    return arr.reduce(function(prev, elem, key) {
        return prev
        .then(function(val) {
            return iter(val, elem, key);
        });
    }, Q(base));
}
416988
protected function normalizeCollection($collection, $name, $format = null, array $context = array())
    {
        if (empty($collection)) {
            return array();
        }

        if ($collection instanceof AbstractLazyCollection and !$collection->isInitialized()) {
            return array();
        }

        $data = array();
        foreach ($collection as $key => $element) {
            $data[$key] = $this->normalizeAssociation($element, $name, $format, $context);
        }

        return $data;
    }
768360
public function lpush($collection, $key, $value) {

        $list = $this->getKey($collection, $key, []);

        array_unshift($list, $value);

        $this->setKey($collection, $key, $list);

        return count($list);
    }
625261
public function createGroup(
        $query_name = "",
        $in_group = false
    ) {
        $this->groups[] = new ReporticoGroup($query_name, $this);
        end($this->groups);
        $ky = key($this->groups);
        return ($this->groups[$ky]);
    }
374028
public function initial($collection=null, $n=null) {
    list($collection, $n) = self::_wrapArgs(func_get_args(), 2);
    
    $collection = (array) self::_collection($collection);
    
    if(is_null($n)) $n = 1;
    $first_index = count($collection) - $n;
    $__ = new self;
    return self::_wrap($__->first($collection, $first_index));
  }
691356
public function getGroup($group_name, $selection_name = 'group')
    {
        $selection_key = $group_name.'_'.$selection_name;

        if(!in_array($group_name, $this->item_names))
        {
            $this->notFoundAction($selection_key);
        }

        if(!array_key_exists($selection_key, $this->selection_items))
        {
            $subType = $this->refFilter->getSub($group_name);
            $this->selection_items[$selection_key] = $this->loader->loadGroupCollectionAsSub($subType, $selection_name, $this->refFilter);

            if($selection_name === 'group')
            {
                $this->standart_items[$group_name] = $this->selection_items[$selection_key];
            }
        }

        return $this->selection_items[$selection_key];
    }
1179450
function reduce(collection, reducer, reduction, context, useFirst, reverse) {
  assertNotInfinite(collection.size);
  collection.__iterate((v, k, c) => {
    if (useFirst) {
      useFirst = false;
      reduction = v;
    } else {
      reduction = reducer.call(context, reduction, v, k, c);
    }
  }, reverse);
  return reduction;
}
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;
    }
891443
private function reduceCursorForGroups()
    {
        $reduceForGroups = $groupKeys = [];
        $index = 0;
        foreach ($this->collectionCursor as $model) {
            $groupKey = $this->getGroupKeyForModel($model);
            if ($groupKey != '') {
                if (!in_array($groupKey, $groupKeys)) {
                    $groupKeys[] = $groupKey;
                } else {
                    $reduceForGroups[] = $index;
                }
            }
            $index++;
        }

        if (count($reduceForGroups) > 0) {
            $this->collectionCursor->filterModelsByIndex($reduceForGroups);
        }
    }
806379
public function reduce($reduce)
    {
        switch ($this->query['type']) {
            case Query::TYPE_MAP_REDUCE:
                $this->query['mapReduce']['reduce'] = $reduce;
                break;

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

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

        return $this;
    }
1037070
public function getGroupCustom($group_name, $ref_name, $selection_name = 'group')
    {
        return $this->subVars->getCollectionSet($ref_name)->getGroup($group_name, $selection_name);
    }
523495
public function groupBy($groupBy, $preserveKeys = false)
    {
        if (is_array($groupBy)) {
            $nextGroups = $groupBy;

            $groupBy = array_shift($nextGroups);
        }

        $groupBy = $this->valueRetriever($groupBy);

        $results = [];

        foreach ($this->items as $key => $value) {
            $groupKeys = $groupBy($value, $key);

            if (! is_array($groupKeys)) {
                $groupKeys = [$groupKeys];
            }

            foreach ($groupKeys as $groupKey) {
                $groupKey = is_bool($groupKey) ? (int) $groupKey : $groupKey;

                if (! array_key_exists($groupKey, $results)) {
                    $results[$groupKey] = new static;
                }

                $results[$groupKey]->offsetSet($preserveKeys ? $key : null, $value);
            }
        }

        $result = new static($results);

        if (! empty($nextGroups)) {
            return $result->map->groupBy($nextGroups, $preserveKeys);
        }

        return $result;
    }
639074
public function hasKey($key, $collection)
    {
        if (empty($this->queriesResultCache[ 'collectionKeys' ][ $collection ])) {
            $this->getKeys($collection);
        }

        return (bool)in_array($key, $this->queriesResultCache[ 'collectionKeys' ][ $collection ]);
    }