本文整理汇总了PHP中MongoCollection::aggregate方法的典型用法代码示例。如果您正苦于以下问题:PHP MongoCollection::aggregate方法的具体用法?PHP MongoCollection::aggregate怎么用?PHP MongoCollection::aggregate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MongoCollection
的用法示例。
在下文中一共展示了MongoCollection::aggregate方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getPictureTotalSize
public function getPictureTotalSize()
{
$result = $this->collection->aggregate([['$match' => ['-class' => $this->alias]], ['$project' => ['size' => true, '-class' => true]], ['$group' => ['_id' => '$-class', 'total' => ['$sum' => '$size']]]]);
$total = 0;
if ($result['ok'] == 1 && count($result['result'])) {
$total = $result['result'][0]['total'];
}
return $total;
}
示例2: favouriteRecipients
/**
*/
public function favouriteRecipients($limit, $filter = null)
{
$query = array(self::SUCCESS => 1, self::WHO => $GLOBALS['registry']->getAuth());
if (!empty($filter)) {
$query[self::ACTION] = array('$in' => $filter);
}
$out = array();
try {
$res = $this->_db->aggregate(array(array('$match' => $query), array('$group' => array('_id' => '$' . self::RECIPIENT, 'count' => array('$sum' => 1))), array('$sort' => array('count' => -1)), array('$limit' => $limit)));
if (isset($res['result'])) {
foreach ($res['result'] as $val) {
$out[] = $val['_id'];
}
}
} catch (MongoException $e) {
}
return $out;
}
示例3: aggregate
/**
* @param array $pipeline
* @param array $op
* @param null $op1
* @return array
*/
public function aggregate($pipeline, $op = null, $op1 = null)
{
$op = $op ?: [];
if (is_array($op) && isset($op['cache']) && $op['cache'] === false) {
unset($op['cache']);
return parent::aggregate($pipeline, $op);
}
if (!isset($pipeline[0])) {
$pipeline = func_get_args();
}
$hash = $this->hash($pipeline);
$result = $this->cache->get($hash);
if (!$result) {
$result = parent::aggregate($pipeline, $op);
$this->cache->set($hash, $result, $this->getCacheTime($op));
}
return $result;
}
示例4: getFresh
/**
* Execute the query as a fresh "select" statement.
*
* @param array $columns
* @return array|static[]
*/
public function getFresh($columns = array())
{
// If no columns have been specified for the select statement, we will set them
// here to either the passed columns, or the standard default of retrieving
// all of the columns on the table using the "wildcard" column character.
if (is_null($this->columns)) {
$this->columns = $columns;
}
// Drop all columns if * is present, MongoDB does not work this way.
if (in_array('*', $this->columns)) {
$this->columns = array();
}
// Compile wheres
$wheres = $this->compileWheres();
// Use MongoDB's aggregation framework when using grouping or aggregation functions.
if ($this->groups or $this->aggregate) {
$group = array();
// Add grouping columns to the $group part of the aggregation pipeline.
if ($this->groups) {
foreach ($this->groups as $column) {
$group['_id'][$column] = '$' . $column;
// When grouping, also add the $last operator to each grouped field,
// this mimics MySQL's behaviour a bit.
$group[$column] = array('$last' => '$' . $column);
}
} else {
// If we don't use grouping, set the _id to null to prepare the pipeline for
// other aggregation functions.
$group['_id'] = null;
}
// Add aggregation functions to the $group part of the aggregation pipeline,
// these may override previous aggregations.
if ($this->aggregate) {
$function = $this->aggregate['function'];
foreach ($this->aggregate['columns'] as $column) {
// Translate count into sum.
if ($function == 'count') {
$group['aggregate'] = array('$sum' => 1);
} else {
$group['aggregate'] = array('$' . $function => '$' . $column);
}
}
} else {
foreach ($this->columns as $column) {
$key = str_replace('.', '_', $column);
$group[$key] = array('$last' => '$' . $column);
}
}
// Build the aggregation pipeline.
$pipeline = array();
if ($wheres) {
$pipeline[] = array('$match' => $wheres);
}
$pipeline[] = array('$group' => $group);
// Apply order and limit
if ($this->orders) {
$pipeline[] = array('$sort' => $this->orders);
}
if ($this->offset) {
$pipeline[] = array('$skip' => $this->offset);
}
if ($this->limit) {
$pipeline[] = array('$limit' => $this->limit);
}
if ($this->projections) {
$pipeline[] = array('$project' => $this->projections);
}
// Execute aggregation
$results = $this->collection->aggregate($pipeline);
// Return results
return $results['result'];
} else {
if ($this->distinct) {
// Return distinct results directly
$column = isset($this->columns[0]) ? $this->columns[0] : '_id';
// Execute distinct
$result = $this->collection->distinct($column, $wheres);
return $result;
} else {
$columns = array();
// Convert select columns to simple projections.
foreach ($this->columns as $column) {
$columns[$column] = true;
}
// Add custom projections.
if ($this->projections) {
$columns = array_merge($columns, $this->projections);
}
// Execute query and get MongoCursor
$cursor = $this->collection->find($wheres, $columns);
// Apply order, offset and limit
if ($this->timeout) {
$cursor->timeout($this->timeout);
}
//.........这里部分代码省略.........
示例5: getFresh
/**
* Execute the query as a fresh "select" statement.
*
* @param array $columns
* @return array|static[]
*/
public function getFresh($columns = [])
{
// If no columns have been specified for the select statement, we will set them
// here to either the passed columns, or the standard default of retrieving
// all of the columns on the table using the "wildcard" column character.
if (is_null($this->columns)) {
$this->columns = $columns;
}
// Drop all columns if * is present, MongoDB does not work this way.
if (in_array('*', $this->columns)) {
$this->columns = [];
}
// Compile wheres
$wheres = $this->compileWheres();
// Use MongoDB's aggregation framework when using grouping or aggregation functions.
if ($this->groups or $this->aggregate or $this->paginating) {
$group = [];
// Add grouping columns to the $group part of the aggregation pipeline.
if ($this->groups) {
foreach ($this->groups as $column) {
$group['_id'][$column] = '$' . $column;
// When grouping, also add the $last operator to each grouped field,
// this mimics MySQL's behaviour a bit.
$group[$column] = ['$last' => '$' . $column];
}
// Do the same for other columns that are selected.
foreach ($this->columns as $column) {
$key = str_replace('.', '_', $column);
$group[$key] = ['$last' => '$' . $column];
}
}
// Add aggregation functions to the $group part of the aggregation pipeline,
// these may override previous aggregations.
if ($this->aggregate) {
$function = $this->aggregate['function'];
foreach ($this->aggregate['columns'] as $column) {
// Translate count into sum.
if ($function == 'count') {
$group['aggregate'] = ['$sum' => 1];
} else {
$group['aggregate'] = ['$' . $function => '$' . $column];
}
}
}
// When using pagination, we limit the number of returned columns
// by adding a projection.
if ($this->paginating) {
foreach ($this->columns as $column) {
$this->projections[$column] = 1;
}
}
// The _id field is mandatory when using grouping.
if ($group and empty($group['_id'])) {
$group['_id'] = null;
}
// Build the aggregation pipeline.
$pipeline = [];
if ($wheres) {
$pipeline[] = ['$match' => $wheres];
}
if ($group) {
$pipeline[] = ['$group' => $group];
}
// Apply order and limit
if ($this->orders) {
$pipeline[] = ['$sort' => $this->orders];
}
if ($this->offset) {
$pipeline[] = ['$skip' => $this->offset];
}
if ($this->limit) {
$pipeline[] = ['$limit' => $this->limit];
}
if ($this->projections) {
$pipeline[] = ['$project' => $this->projections];
}
$options = ['typeMap' => ['root' => 'array', 'document' => 'array']];
// Execute aggregation
$results = iterator_to_array($this->collection->aggregate($pipeline, $options));
// Return results
return $results;
} elseif ($this->distinct) {
// Return distinct results directly
$column = isset($this->columns[0]) ? $this->columns[0] : '_id';
// Execute distinct
if ($wheres) {
$result = $this->collection->distinct($column, $wheres);
} else {
$result = $this->collection->distinct($column);
}
return $result;
} else {
$columns = [];
// Convert select columns to simple projections.
//.........这里部分代码省略.........
示例6: foreach
$productIds = [];
foreach ($products as $product) {
$productIds[] = $product['product_id'];
}
$products = $qb->select('p')->andWhere('p.id IN (:product_ids)')->setParameter(':product_ids', $productIds)->getQuery()->execute();
break;
case 'doctrine/mongodb-odm':
$client = new MongoClient();
$database = $container->getParameter('mongodb_database');
$db = $client->{$database};
$productCollection = new MongoCollection($db, PIM_CATALOG_PRODUCT);
$variantGroupIds = $container->get('pim_catalog.repository.group')->getAllVariantGroupsQB()->select('g.id')->getQuery()->execute(null, AbstractQuery::HYDRATE_ARRAY);
array_walk($variantGroupIds, function (&$value) {
$value = $value['id'];
});
$selectedProducts = $productCollection->aggregate([['$unwind' => '$groupIds'], ['$match' => ['groupIds' => ['$in' => $variantGroupIds]]], ['$group' => ['_id' => '$normalizedData.' . $identifierCode, 'variant_count' => ['$sum' => 1]]], ['$match' => ['variant_count' => ['$gt' => 1]]]])['result'];
$productIdentifiers = [];
foreach ($selectedProducts as $product) {
$productIdentifiers[] = $product['_id'];
}
$products = $container->get('pim_catalog.doctrine.query.product_query_factory')->create()->addFilter($identifierCode, 'IN', $productIdentifiers)->execute();
break;
default:
throw new \LogicException(sprintf('Unknown storage driver %s (supported storage drivers : doctrine/orm and doctrine/mongodb-odm)', $storageDriver));
}
$output = new ConsoleOutput();
if (count($products) > 0) {
$output->writeln(sprintf('%s products are in more than one variant group. This is not permitted anymore', count($products)));
$output->writeln('Products in more than one variant group :');
$lines = [];
$tableHelper = new TableHelper();
示例7: testPipeline
public function testPipeline()
{
$result = $this->collection->aggregate(['$group' => ['_id' => 'foo', 'sum' => ['$sum' => '$foo']]]);
$this->assertEquals(4, $result['result'][0]['sum']);
}
示例8: aggregate
/**
* aggregate框架指令达成
*
* @return mixed
*/
public function aggregate($pipeline, $op = NULL, $op1 = NULL)
{
if (!$this->_noAppendQuery) {
if (isset($pipeline[0]['$geoNear'])) {
$first = array_shift($pipeline);
array_unshift($pipeline, array('$match' => array('__REMOVED__' => false)));
array_unshift($pipeline, $first);
} else {
array_unshift($pipeline, array('$match' => array('__REMOVED__' => false)));
}
}
return parent::aggregate($pipeline);
}
示例9: mkonegrp
function mkonegrp($db, $colname, $query, $grouparr)
{
$ops = array();
$ops[] = ['$match' => $query];
// $ops[] = ['$sort' => ['year'=> -1, 'month'=> -1]];
$ops[] = ['$group' => $grouparr];
$option = ['allowDiskUse' => true];
//print_r($ops);
$collection = new MongoCollection($db, $colname);
echo "working on: {$colname} ... with";
$col2name = $colname . "_agg";
$col2 = new MongoCollection($db, $col2name);
//print_r($grouparr['_id']);
//makegrpIndex($db, $col2, $grouparr['_id']);
try {
$cursor = $collection->aggregate($ops, $option);
} catch (MongoException $e) {
echo "error message: " . $e->getMessage() . "\n";
echo "error code: " . $e->getCode() . "\n";
exit(1);
}
$results = $cursor['result'];
foreach ($results as $key => $val) {
foreach ($val as $skey => $sval) {
if ($skey == '_id') {
$r = $sval;
} else {
$r[$skey] = $sval;
}
}
//print_r($r);
$col2->insert($r);
}
}