Mongodb select all fields group by one field and sort by another field

前端 未结 4 641
灰色年华
灰色年华 2021-02-09 03:44

We have collection \'message\' with following fields

_id |   messageId |  chainId | createOn

1   |       1     |    A     | 155
2   |       2     |    A     | 1         


        
4条回答
  •  感情败类
    2021-02-09 04:17

    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);
    

提交回复
热议问题