之前我有以下聚合管道。
db.EXCEPTIONS.aggregate(
[{ $match : {
$and : [
{workflow_stage_current_assignee : {$ne:null}},
{CreatedDate:{$gt:ISODate("2017-07-12")}},
{CreatedDate:{$lt:ISODate("2018-07-12")}}
]}
},
{
$group : {_id: {user : "$workflow_stage_current_assignee", cm:"$control_monitor"},
exceptions:{$sum:1} }
},
{ $project : {"user":1,"cm":1,"exceptions":1}},
{ $sort : {"exceptions":1}}
]);
对应的Mongo模板转换如下。
Calendar fromDate = Calendar.getInstance();
fromDate.set(2016, 7, 12, 0, 0, 0);
Calendar toDate = Calendar.getInstance();
toDate.set(2018, 7, 12, 23, 59, 59);
MatchOperation match = Aggregation.match(new Criteria("workflow_stage_current_assignee").ne(null)
.andOperator(new Criteria("CreatedDate").gte(new Date(fromDate.getTimeInMillis()))
.andOperator(new Criteria("CreatedDate").lte(new Date(toDate.getTimeInMillis())))));
GroupOperation group = Aggregation.group("workflow_stage_current_assignee","control_monitor").count().as("exceptions");
ProjectionOperation project = Aggregation.project("workflow_stage_current_assignee","control_monitor","exceptions");
SortOperation sort=Aggregation.sort(Sort.Direction.DESC,"exceptions");
Aggregation aggregation = Aggregation.newAggregation(match,group,project,sort);
AggregationResults<ExceptionCount> output = mongoTemplate.aggregate(aggregation, "EXCEPTIONS", ExceptionCount.class);
这个之前运作得很好。然后我修改了原始查询,加入了额外的 overdue 分组选项。
$group : {_id: {user : "$workflow_stage_current_assignee", cm:"$control_monitor"},
exceptions:{$sum:1},
},
overdue: {$sum : {$cond :[
{$gt:["$CreatedDate",ISODate("2017-07-12")]},1,0
]}
}
但我不知道如何在Mongo Template查询中实现这个额外的分组条件。我在网上搜索了一下,但大多数结果都使用较旧API的DBObject。有人可以帮助我在Mongo Template中完成吗?