Spring data mongodb sort on multiple fields Spring data mongodb sort on multiple fields mongodb mongodb

Spring data mongodb sort on multiple fields


You can try something like this.

Aggregation agg = newAggregation(        match(Criteria.where("userId").is(userId)),        sort(Sort.Direction.DESC, "type").and(Sort.Direction.ASC, "createdDate"));


Little bit late, but for other people...Try this (for spring-data):

private static final Sort NOTE_SORT = new Sort(new Sort.Order(Sort.Direction.ASC, "seen"),                                                new Sort.Order(Sort.Direction.DESC, "date"),                                                new Sort.Order(Sort.Direction.ASC, "done"));


You can create order list and use it for sort like this

List<Order> orders = new ArrayList<>();orderQuery.add(new Order(Direction.DESC, "createdDate"));Sort sorts = new Sort(orders.toArray(new Order[orders.size()]));    Aggregation agg = newAggregation(        match(Criteria.where("userId").is(userId)),        sort(sorts));