Criteria criteria = new Criteria();
Date date = new Date();
Date startDate = DateUtils.getMonthFirstDay(date);
Date endDate = DateUtils.getMonthEndDay(date);
criteria.andOperator(Criteria.where(“id”).is(1),
Criteria.where("create_date").gte(startDate),
Criteria.where("create_date").lt(endDate)
);
MatchOperation matchOperation = Aggregation.match(criteria);
GroupOperation groupOperation = Aggregation.group("id","groupDate")
.max("amount").as("maxAmount");
Aggregation aggregation = Aggregation.newAggregation(Test.class, Aggregation.project("id", "create_date", "amount").and(DateOperators.DateToString.dateOf("create_date").toString("%Y-%m-%d")).as("groupDate"), matchOperation, groupOperation);
AggregationResults<Document> state = null;
try {
state = mongoTemplate.aggregate(aggregation, "t_test", Document.class);
} catch (Exception e) {
log.error("---------AggregationResults>:", e);
}
Java MongoDB group by date 根据指定时间字符串格式分组
最新推荐文章于 2024-04-23 18:52:22 发布