mongodb - 带有教义-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);
解决方案
$qb->group()
->field('id')->expression(
$qb->expr()->field('department')->expression('$department')
)
->sum('$department');
下次使用 $qb->getPipeline() 来调试你的查询,它会为你节省很多时间。
推荐阅读
- c++ - 指向向量中对象的共享指针
- logstash - 如何在不同的关键字下将日志输出到不同的Kafka主题?
- android - How to achieve this custom marker?
- javascript - How can I use clipboard.js with a span tag onselect event?
- esoteric-languages - Befunge 程序在 @ 处意外循环
- c++ - 为什么 int x{ y = 5 } 可能?
- html - 盒子定位包装
- php - DataFixtures phpunit symfony 类型的预期值改为“整数”
- reactjs - 常规函数中的 Redux 钩子(钩子规则)
- sql - SQL:聚合总和和过滤的聚合总和