Spring-data-mongodb來進行操作Mongodb進行分組、統計等操作示例
Mongodb分組統計查詢
@Override
public List countDialogGroupByParam(Date startDate, Date endDate, UserSourceEnum userSourceEnum, String source, DialogStatusEnum status) {
// 返回的欄位
ProjectionOperation projectionOperation = Aggregation.project(“csUserSource”, “initSource”, “status”,“count”);
Criteria criteria =new Criteria(“beginTime”).gte(startDate).lte(endDate);
MatchOperation matchOperation = Aggregation.match(criteria);
//分組的欄位
GroupOperation groupOperation = Aggregation.group(“csUserSource”, “initSource”, “status”).count().as(“count”);
Aggregation aggregation = Aggregation.newAggregation(projectionOperation, matchOperation, groupOperation);
AggregationResults aggregationResults = super.getMongoTemplate().aggregate(aggregation, “JtkDialog”, DialogCountDomain.class);
return aggregationResults.getMappedResults();
}