如何在 Spring Data MongoDB 中表达复杂的 $sum 分组表达式 [英] How to express complex $sum grouping expression in Spring Data MongoDB
问题描述
我有以下在 MongoDB 中运行良好的 MongoDB 聚合查询
<预><代码> [{ $match: { "myfield":"X" },{ $组:{_id: { myfield: "$myfield" },计数: { $sum: 1 },lt5w: { $sum: { $cond:{ if: { $gte: [ "$myDate", new Date(ISODate().getTime() - 1000 * 60 * 60 * 24 * 7 * 5) ] },然后: 1, 否则: 0 } } },gt12w: { $sum: { $cond:{ if: { $gte: [ new Date(ISODate().getTime() - 1000 * 60 * 60 * 24 * 7 * 12), "$myDate" ] }, 然后: 1, 否则: 0 } } }}}])如何使用 Spring Data MongoDB API 表达这种复杂的 $sum 操作?
group("myfield)).sum("???").as("lt5w").sum("???").as("gt12w").count().as("count"),
sum() 方法只需要简单的字符串.根据这张票(已关闭)https://jira.spring.io/browse/DATAMONGO-784聚合应该支持复杂的操作,比如 $cmp 和 $cond
更新:似乎忘记了方法的 sum(AggregationExpression expr) 版本 这里.min(), max(), first() 有那个方法版本.
提交工单,问题已解决!
https://jira.spring.io/browse/DATAMONGO-1784
I have following MongoDB aggregation query that works well in MongoDB
[
{ $match: { "myfield":"X" },
{ $group: {
_id: { myfield: "$myfield" },
count: { $sum: 1 },
lt5w: { $sum: { $cond:{ if: { $gte: [ "$myDate", new Date(ISODate().getTime() - 1000 * 60 * 60 * 24 * 7 * 5) ] }, then: 1, else: 0 } } },
gt12w: { $sum: { $cond:{ if: { $gte: [ new Date(ISODate().getTime() - 1000 * 60 * 60 * 24 * 7 * 12), "$myDate" ] }, then: 1, else: 0 } } }
}
}
])
How can I express this complex $sum operation using Spring Data MongoDB API?
group("myfield))
.sum("???").as("lt5w")
.sum("???").as("gt12w")
.count().as("count"),
The sum() method only expects simple string. According to this ticket (closed) https://jira.spring.io/browse/DATAMONGO-784 the aggregation should support complex operations like $cmp and $cond
Update: It seems that the sum(AggregationExpression expr) version of the method is forgotten here. min(), max(), first() have that method version.
Filed a ticket and the issue is fixed!
https://jira.spring.io/browse/DATAMONGO-1784
这篇关于如何在 Spring Data MongoDB 中表达复杂的 $sum 分组表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!