首页 > 解决方案 > 带有教义-odm 聚合的 symfony4 不起作用

问题描述

我已将 mongo shell 查询转换为 symfony4 学说 odm 查询。为此,我正在使用createAggregationBuilder。它不会返回任何东西并抛出内存异常。

注意: 即使我也增加了 php 限制,也面临同样的问题。

请找到以下代码:

壳蒙戈查询:

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

壳蒙戈输出:

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

与在 symfony4 中创建的相同。但我正面临这个问题。

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

标签: mongodbsymfony4

解决方案


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

下次使用 $qb->getPipeline() 来调试你的查询,它会为你节省很多时间。


推荐阅读