mongodb某个字段distinct计数问题

List<AggregationOperation> operations = new ArrayList<>();
operations.add(Aggregation.group("user_id"));
operations.add(Aggregation.count().as("total"));
Aggregation agg = Aggregation.newAggregation(operations);
TotalDto dto = mongoTemplate.aggregate(agg, "集合名称", TotalDto.class).getUniqueMappedResult();
if (Objects.nonNull(dto)) {
	return dto.getTotal();
}
return 0L;
posted @ 2023-03-23 17:59  沙漠皇帝  阅读(96)  评论(0编辑  收藏  举报