5

I want to sort the returned result for each JSP page(100 items each page),not a global sort.

   DBObject sort = new BasicDBObject();
   DBObject exist = new BasicDBObject();
   DBObject query= new BasicDBObject();
   exist.put("$exists",1);
   query.put("sortKey":exist);//sortKey is not indexed
   sort.put("sortKey",1);
    DBCursor cursor = dbcollection.find(query).limit(100).sort(sort);
    while(cursor.hasNext()){
    System.out.println(cursor.next());
    }

But in fact ,the sort is stilled processed for all the documents in the collection,namely ,it is a global sort even though I use function limit(100) .Since the collection is very large-scale , the sort function will take quite a long time.So , I wonder if mongodb java driver has a feature which will perform a local(just sort for the returned 100 documents) instead of global sort?

5
  • Are you sure you want to sort the documents locally? Without a defined sort order the results in the limit() query aren't going to be predictable. Using an index to sort query results would make those server-side queries run efficiently with your limit() and give you a predictable ordering by sortKey. Commented Jan 5, 2014 at 5:06
  • What do you mean by "Without a defined sort order the results in the limit() query aren't going to be predictable" ? If the data are all static data , then every time I use query().skip().limit(),the returned result could be different? Commented Jan 6, 2014 at 0:52
  • Assuming there are active inserts/updates/deletes on this collection, the results could be different between successive queries as documents are relocated (due to document growth) or freed space from deleted documents is reused. If you sort on the client side the next page(s) may have items that do not appear in the logical order that users would expect. Since you are using this for paginating, your users probably want something more predictable .. like sorting by the sortKey on the server-side so the order is always ascending to match the pagination. Commented Jan 6, 2014 at 1:20
  • Yes ,I know.But in my JSP page,user does not care about the predicable of the result ,what I should do is just providing them a sorted result for every page to make the page looks pretty.My question is ,for a static mongodb database,"without a defined sort order the results in the limit() query aren't going to be predictable" is right? Commented Jan 6, 2014 at 3:33
  • Without using server-side sort, imagine you get 100 documents (page 1) from a collection. They aren't in any specific order, but you can sort those on the client. Now get the next 100 documents (page 2), and sort them on the client. There may be results in page 2 that the user was expecting on page 1 based on the sort key. If the data is unchanging, the results may be consistent but they won't be predictable/expected from the end user point of view. The first 100 documents are the first 100 documents found; the second 100 documents are the next 100 found. Maybe this doesn't affect your usage? Commented Jan 6, 2014 at 3:40

2 Answers 2

4

By using Mongodb 3.x and the corresponding java driver, you sort by doing the following:

List<Document> list = collection.find().sort(descending("number")).into(new ArrayList<Document>());

Usage sort as: sort(order("field"));

order = ascending or descending

Sign up to request clarification or add additional context in comments.

Comments

1

There's no way to sort locally in the Java driver. If you want a local sort, read the documents into an array and sort the array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.