Reputation: 89
I have problems to implement a groupBy with doctrine mongo db.
I followed this two guides:
http://cookbook.mongodb.org/patterns/unique_items_map_reduce/
Exactly, I want to make this groupBy: "select * from fonts where 1 group by family"
This is my code that don't works:
$queryBuilder = $this->createQueryBuilder();
$queryBuilder->group(array('family'), array('count' => 0));
$queryBuilder->reduce('function (obj, prev) { prev.count++; }');
$queryBuilder->sort($sort, $order);
return $queryBuilder;
Thanks in advance.
Upvotes: 2
Views: 3907
Reputation: 1350
The aggregation framework provides an easy way to process records and return computed results.
For example, our document is like this.
class Example
{
/**
* @MongoDB\Field(type="int")
*/
protected $noExample;
/**
* @MongoDB\Field(type="string")
*/
protected $libelle;
/**
* @MongoDB\Field(type="string")
*/
protected $family;
}
In ExampleRepository, we need to create this function.
public function groupByFamily()
{
$qb = $this->createAggregationBuilder('Document\Example');
$qb->group()
->field('id')
->expression('$family')
->field('noExample')
->first('$noExample')
->field('libelle')
->first('$libelle')
->field('family')
->first('$family')
->field('number_record')
->sum(1);
$results = $qb->execute();
return $results;
}
You can read more.
Upvotes: 1
Reputation: 11
You can try:
$queryBuilder = $this->createQueryBuilder();
$queryBuilder->group(array('family' => 1), array('count' => 0));
$queryBuilder->reduce('function (obj, prev) { prev.count += 1;}');
$queryBuilder->sort($sort, $order);
return $queryBuilder;
Upvotes: 1