Reputation: 3548
I am converting mongo queries into symfony4 doctrine ODM queries. In this, doctrine will not accept the multiple group by fields. How to solve the below use case?
Mongo Queries:
db.Article.aggregate([
{
$group: {
_id: { department: "$department", status : "$status" },
MIN: { $min: "$salary" },
MAX: { $max: "$salary" }
}
}
]);
Mongo Query Results:
{ "_id" : { "department" : "sales", "status" : 0 }, "MIN" : 100, "MAX" : 5000 }
{ "_id" : { "department" : "sales", "status" : 1 }, "MIN" : 10000, "MAX" : 16500 }
{ "_id" : { "department" : "IT", "status" : 1 }, "MIN" : 5000, "MAX" : 5000 }
{ "_id" : { "department" : "DOCTOR", "status" : 1 }, "MIN" : 20000000, "MAX" : 20000000 }
{ "_id" : { "department" : "IAS", "status" : 1 }, "MIN" : 120000, "MAX" : 120000 }
Symfony4 with doctrine odm :
$qb = $this->createAggregationBuilder($documentClass);
$qb
->group()
->field('id')
->expression('$department')
->field('department')
->first('$department')
->field('status')
->first('$status')
->field('lowestValue')
->min('$salary')
->field('highestValue')
->max('$salary')
->field('totalValue')
->sum(1);
It will only considered group by field of department alone. I want to group by both department and status based group results.
Upvotes: 0
Views: 980
Reputation: 1350
This solution works with Symfony 3. In ArticleRepository.php file write this lines:
$qb = $this->createAggregationBuilder('Document\Article');
$group_expression = $qb->expr()
->field('departement')
->expression('$department')
->field('max')
->expression('$max')
->field('min')
->expression('$min');
$qbf->group()
->field('id')
->expression($group_expression)
->field('department')
->first('$department')
->field('status')
->first('$status')
->field('lowestValue')
->min('$salary')
->field('highestValue')
->max('$salary')
->field('totalValue')
->sum(1);
Upvotes: 2