java mongodb sort()和limit()函数

wuc*_*ang 5 java sorting mongodb

我想对每个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());
    }
Run Code Online (Sandbox Code Playgroud)

但是实际上,对于集合中的所有文档,仍对排序进行处理,即,即使使用函数limit(100),它也是全局排序。由于集合的规模非常大,因此排序功能将占用相当大的空间。因此,我想知道mongodb java驱动程序是否具有将执行本地(仅对返回的100个文档进行排序)而不是全局排序的功能?

Eve*_*man 1

无法在 Java 驱动程序中进行本地排序。如果您想要本地排序,请将文档读入数组并对数组进行排序。