How to express complex $sum grouping expression in Spring Data MongoDB

自闭症网瘾萝莉.ら 提交于 2019-12-11 15:28:31

问题


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.


回答1:


Filed a ticket and the issue is fixed!

https://jira.spring.io/browse/DATAMONGO-1784



来源:https://stackoverflow.com/questions/46270910/how-to-express-complex-sum-grouping-expression-in-spring-data-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!