We have collection \'message\' with following fields
_id | messageId | chainId | createOn
1 | 1 | A | 155
2 | 2 | A | 1
here is the solution using springframework.data.mongodb:
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.group("chainId"),
Aggregation.sort(new Sort(Sort.Direction.ASC, "createdOn"))
);
AggregationResults results = mongoTemplate.aggregate(aggregation, "collection_name", XxxBean.class);