Quiero ordenar varios campos en MongoDB usando Spring data para MongoDB. Actualmente estoy tratando de lograr esto usando la agregación:
Aggregation agg = newAggregation( match(Criteria.where("userId").is(userId)), sort(Sort.Direction.DESC, "type", "createdDate"), ); AggregationResults<MyBean> results = mongoOperations.aggregate(agg, MyBean.class, MyBean.class); Cuando estoy haciendo esto, está ordenando el type y la fecha de createdDate en el orden DESC . Pero quiero DESC en type y ASC en createdDate .
Lo intenté,
sort(Sort.Direction.DESC, "type"); sort(Sort.Direction.ASC, "createdDate"); pero esto está ordenando solo en createdDate .
Puedes probar algo como esto.
Aggregation agg = newAggregation( match(Criteria.where("userId").is(userId)), sort(Sort.Direction.DESC, "type").and(Sort.Direction.ASC, "createdDate") );Un poco tarde, pero para otras personas... Prueba esto (para datos de primavera):
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"));Puede crear una lista de pedidos y usarla para ordenar como esta
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) );