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?
limit()query aren't going to be predictable. Using an index to sort query results would make those server-side queries run efficiently with yourlimit()and give you a predictable ordering bysortKey.sortKeyon the server-side so the order is always ascending to match the pagination.