我正在使用spring-data-mongodb v1.10.3 聚合查询在cli / robo3t中有效,但我似乎无法将其转换为Spring。
>>> re.findall(r'(?:^|[ \t]+)([1-9]|[1-2][0-9]|30)(?=[ \t]|$)',' '.join([str(i) for i in range(-5000,5000)]), re.M)
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30']
Spring代码在不引发任何错误的意义上“有效”,但是返回空结果。
db.getCollection('transaction').aggregate(
[
{
$match : {
effectiveDate : {
$gte : ISODate("2018-09-06T04:00:00.000Z") , // Spring aggregation puts these clauses as $gte : { $date: <date> }
$lte : ISODate("2018-09-11T03:59:59.000Z")
} ,
status : "external_received"}
}
,
{
$group : {
_id : { portfolio : "$portfolio" , processor : "$processor" , transactionType : "$transactionType"} ,
amount : { $sum : "$amount"}
}
}
]
)
更新:
Spring代码在public List<TransactionReport> getTransactionReport(Date begin, Date end) {
Aggregation aggregation = portfolioAggregation(begin, end);
System.out.println(aggregation.toString());
AggregationResults<TransactionReport> results = mongoTemplate.aggregate(aggregation, Transaction.class, TransactionReport.class);
return results.getMappedResults();
}
private Aggregation portfolioAggregation(Date begin, Date end) {
Criteria f = Criteria.where("effectiveDate").gte(begin).lte(end).and("status").is("received");
MatchOperation filter = match(f);
GroupOperation groupOperation = group("portfolio", "processor", "transactionType").sum("amount").as("amount");
return Aggregation.newAggregation(filter, groupOperation).withOptions(Aggregation.newAggregationOptions().cursor(getCursor()).build());
}
private DBObject getCursor() {
DBObject cursor = new BasicDBObject();
cursor.put("batchSize", 10);
return cursor;
}
结构为results.getRawResults()
仍然想出一种将结果优雅地序列化到TransactionReport类的方法