在mongo db中使用Java驱动程序时,如何限制结果数?

lon*_*gda 4 java grails groovy mongodb

http://api.mongodb.org/java/2.1/com/mongodb/DBCollection.html#find(com.mongodb.DBObject,com.mongodb.DBObject,int,int)

将它与Grails和mongo db插件一起使用.

这是我正在使用的代码...不知道为什么但光标返回整个数据集.在这种情况下,我只是想尝试返回前20个匹配项(is_processed = false):

def limit = {
    def count = 1;
    def shape_cursor = mongo.shapes.find(new BasicDBObject("is_processed", false),new BasicDBObject(),0,20);
    while(shape_cursor.hasNext()){
        shape_cursor.next();
        render "<div>" + count + "</div"
        count++;
    }
}
Run Code Online (Sandbox Code Playgroud)

有人有想法吗?

Ale*_*rov 5

limit是一种方法DBCursor:DBCursor.limit(n).

所以你只需要做

def shape_cursor = mongo.shapes.find(...).limit(20);
Run Code Online (Sandbox Code Playgroud)