1、年月查询(Aggregation)
Criteria criteria = new Criteria(); criteria.and("enId").in(organIds); criteria.and("updateBy").is(Integer.parseInt(year)); criteria.and("createBy").is(Integer.parseInt(month)); Aggregation aggregation = Aggregation.newAggregation( // 第一步:挑选所需的字段,类似select *,*所代表的字段内容 Aggregation.project("id","enId","enFilesId","createBy","updateBy") .andExpression("{$month: '$createTime'}").as("createBy") .andExpression("{$year: '$createTime'}").as("updateBy"), // 第二步:sql where 语句筛选符合条件的记录 Aggregation.match(criteria), // 第三步:分组条件,设置分组字段 id为enId,enId为数量,enFilesId Aggregation.group("enId").count().as("enId").first("enFilesId").as("enFilesId") // 第四步:重新挑选字段 // Aggregation.project("mn", "sn", "sumTp") ); AggregationResults<AbInfo> results_ = mongoTemplate.aggregate(aggregation, AbInfo.class, AbInfo.class); //循环 for(AbInfo abInfo : results_){ }