• Jobs
  • About Us
  • Jobs
    • Home
    • Jobs
    • Courses and challenges
  • Businesses
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

593
Views
Spring data mongodb ordenar en múltiples campos

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 .

over 3 years ago · Santiago Trujillo
3 answers
Answer question

0

Puedes probar algo como esto.

 Aggregation agg = newAggregation( match(Criteria.where("userId").is(userId)), sort(Sort.Direction.DESC, "type").and(Sort.Direction.ASC, "createdDate") );
over 3 years ago · Santiago Trujillo Report

0

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"));
over 3 years ago · Santiago Trujillo Report

0

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) );
over 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Show me some job opportunities
There's an error!