问题
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