Results 1 to 6 of 6

Thread: spring-data mongodb and sorting with multiple fields

  1. #1

    Default 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. #2
    Join Date
    Aug 2004
    Posts
    1,107

    Default

    Have you tried:

    Code:
    query.sort().on("clicks", Order.DESCENDING).on("date", Order.DESCENDING);
    If so what is the result?
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

  3. #3

    Default

    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 at 11:54 PM.

  4. #4

    Default

    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. #5
    Join Date
    Jun 2011
    Posts
    1

    Default

    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. #6
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    483

    Default

    Fixed and will be available in tonights snapshot: https://jira.springsource.org/browse/DATADOC-177

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •