Announcement Announcement Module
No announcement yet.
spring-data mongodb and sorting with multiple fields Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • spring-data mongodb and sorting with multiple fields

    I have two fields I want to sort with

    (order by clicks descending, date descending)

    query.sort().on("clicks", Order.DESCENDING);
    query.sort().on("date", Order.DESCENDING);

    With this it won't work, date is always messing the whole sort. Both work separately, but not together. I might be doing this the wrong way, but didn't come up with anything else.

    Is anyone else doing something like this?

  • #2
    Have you tried:

    query.sort().on("clicks", Order.DESCENDING).on("date", Order.DESCENDING);
    If so what is the result?


    • #3
      Yes, I've tried, the result is the same.
      For some reason the sort with date is always the number one sort.

      Behauviour of these are exactly the same:
      query.sort().on("date", Order.DESCENDING).on("clicks", Order.DESCENDING);
      query.sort().on("clicks", Order.DESCENDING).on("date", Order.DESCENDING);
      Last edited by jelinden; Apr 6th, 2011, 11:54 PM.


      • #4
        Really strange, made a simplified test to reproduce this, but it worked correctly.

        As a last resort I added an index:
        mongoTemplate.ensureIndex("collection", new Index("clicks", Order.DESCENDING).on("date", Order.DESCENDING));

        this didn't help straight away. But by accident I removed .on("date", Order.DESCENDING); from the actual search sort ... and ... it works now.


        • #5
          I don't know if you ever solved this or not. But in case someone else runs into it, I solved it by modifying the Sort class to use a LinkedHashMap so that the order would be maintained. I also entered a jira ticket so hopefully it will be fixed in the next version.


          • #6
            Fixed and will be available in tonights snapshot: