Get sorted distinct values with MongoTemplate
问题 I am trying to get list of sorted distinct fields: public List<Object> getDistinctValues(String collection, String fieldName) { Query query = new Query(); query.with(new Sort(Sort.Direction.ASC, fieldName)); return mongoTemplate.findDistinct(query, fieldName, collection, Object.class); } but sorting isn't applied. Is there any way to do it with mongoTemplate? spring-boot-starter-data-mongodb: 2.1.2.RELEASE 回答1: Based on previous answer I solved my problem with Mongo Aggregation: @Override