Elangovan
Elangovan

Reputation: 3548

symfony4 with doctrine-odm aggregation is not working

I have converted the mongo shell query into symfony4 doctrine odm queries. For this, i am using the createAggregationBuilder. It will not return anything and throwing out of memory exception.

Note : Even i have increase the php limit too, facing the same issue.

Please find the below codes:

Shell Mongo Queries:

db.Article.aggregate([
    {
        $group: {
            _id: { department: "$department" },
            count: { $sum: 1 }
        }
    },
    { 
        $sort: { 
            salary: -1 
        } 
    }
]);

Shell Mongo Output:

{ "_id" : { "department" : "IAS" }, "count" : 1 }
{ "_id" : { "department" : "DOCTOR" }, "count" : 1 }
{ "_id" : { "department" : "IT" }, "count" : 1 }
{ "_id" : { "department" : "sales" }, "count" : 4 }

The same as have created in symfony4. but i am facing the issue.

$builder = $this->createAggregationBuilder(Article::class);
$builder
    ->group()
        ->field('id')            
        ->expression('$department')
        ->field('department')
        ->sum('$department')
    ->sort(['salary' => -1])
    ->limit(1);

Upvotes: 1

Views: 829

Answers (1)

Mihail0v
Mihail0v

Reputation: 103

    $qb->group()
        ->field('id')->expression(
            $qb->expr()->field('department')->expression('$department')
        )
        ->sum('$department');

Next time use $qb->getPipeline() to debug your query, it will save you a lot of time.

Upvotes: 3

Related Questions