[英]java mongodb sort() and limit() function
我想對每個JSP頁面的返回結果進行排序(每頁100個項目),而不是全局排序。
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());
}
但是實際上,對於集合中的所有文檔,仍對排序進行處理,即,即使我使用函數limit(100),它也是全局排序。由於集合的規模非常大,排序函數將占用相當大的空間。因此,我想知道mongodb java驅動程序是否具有將執行本地(僅對返回的100個文檔進行排序)而不是全局排序的功能?
通過使用Mongodb 3.x和相應的Java驅動程序,您可以通過執行以下操作進行排序:
List<Document> list = collection.find().sort(descending("number")).into(new ArrayList<Document>());
用法排序為:sort(order(“ field”));
順序=升序或降序
無法在Java驅動程序中進行本地排序。 如果要進行本地排序,請將文檔讀入數組並對該數組進行排序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.