当前位置:嗨网首页>书籍在线阅读

07-结果集分页

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

7.3.3 结果集分页

使用skip()和limit()对MongoDB集合中的文档进行分页 在本节中,您将编写一个MongoDB shell脚本,对来自示例数据集的一系列单词进行分页。通过这个练习,您将熟悉一种对大型数据集进行分页的方法。程序清单7.9所示的示例执行各种find()操作,使用sort()对游标中的文档进行排序,再使用skip()和limit()进行分页。 方法displayWords()设置单词的输出格式,并使用方法map()迭代游标。请注意,数据集每页的单词都不同。 请执行下面的步骤来编写这个脚本,将示例数据集中以w打头的单词进行分页。 1.确保启动了MongoDB服务器。 2.确保运行了生成数据库words的脚本文件code/hour05/generate_words.js。 3.在文件夹code/hour07中新建一个文件,并将其命名为find_paging.js。 4.在这个文件中输入程序清单7.9所示的代码。这些代码连接到数据库words,获取表示集合word_stats的Collection对象,使用find()来获取以w打头的单词,再将这些单词分页——每页10个。 5.将这个文件存盘。 6.打开一个控制台窗口,并切换到目录code/hour07。 7.执行下面的命令以运行这个脚本,它对集合word_stats中的一个大型文档集进行分页。程序清单7.10显示了这个脚本的输出。 程序清单7.9 find_paging.js:在MongoDB shell中对Cursor对象表示的文档进行分页 程序清单7.10 find_paging.js-output:在MongoDB shell中对Cursor对象表示的文档进行分页的输出

为减少返回的文档数,一种常见的方法是进行分页。要进行分页,需要指定要在结果集中跳过的文档数,还需限制返回的文档数。跳过的文档数将不断增加,每次的增量都是前一次返回的文档数。

要对一组文档进行分页,需要使用Cursor对象的方法limit()和skip()。方法skip()让您能够指定在返回文档前要跳过多少个文档。

每次获取下一组文档时,都增大方法skip()中指定的值,增量为前一次调用limit()时指定的值,这样就实现了数据集分页。

对大型数据集(尤其是从网站获得的数据集)进行分页时,需要使用find()操作为每一页创建一个游标。请不要为等待后续Web请求的到来而让游标长时间打开,这不是什么好主意,因为可能根本没有后续Web请求。

例如,下面的语句依次查找第1~10个文档、第11~20个文档和第21~30个文档:

cursor = collection.find().sort({name:1});
cursor.limit(10);
cursor.skip(0);
cursor = collection.find().sort({name:1});
cursor.limit(10);
cursor.skip(10);
cursor = collection.find().sort({name:1});
cursor.limit(10);
cursor.skip(20);

对数据进行分页时,务必调用方法sort()来确保数据的排列顺序不变。

▼ Try It Yourself

mongo find_paging.js
01 function displayWords(skip, cursor){
02    print("Page: " + parseInt(skip+1) + " to " +
03            parseInt(skip+cursor.size()));
04    words = cursor.map(function(word){
05       return word.word;
06    });
07    wordStr = JSON.stringify(words);
08    if (wordStr.length > 65){
09       wordStr = wordStr.slice(0, 50) + "...";
10    }
11    print(wordStr);
12 }
13 mongo = new Mongo("localhost");
14 wordsDB = mongo.getDB("words");
15 wordsColl = wordsDB.getCollection("word_stats");
16 cursor = wordsColl.find({first:'w'});
17 count = cursor.size();
18 skip = 0;
19 for(i=0; i < count; i+=10){
20    cursor = wordsColl.find({first:'w'});
21    cursor.skip(skip);
22    cursor.limit(10);
23    displayWords(skip, cursor);
24    skip += 10;
25 }
Page: 1 to 10
["with","won't","we","what","who","would","will","...
Page: 11 to 20
["way","well","woman","work","world","when","while...
Page: 21 to 30
["where","work","without","water","write","word","...
Page: 31 to 40
["within","walk","win","wait","wife","whole","wear...
Page: 41 to 50
["window","well","wrong","west","whatever","wonder...
Page: 51 to 60
["writer","whom","wish","western","wind","weekend"...
Page: 61 to 70
["while","worth","warm","wave","wonderful","wine",...
Page: 71 to 80
["works","wake","warn","wing","winner","welfare","...
Page: 81 to 90
["wrap","warning","wash","widely","wedding","walk"...
Page: 91 to 100
["watch","wet","weigh","wooden","wealth","wage","w...
Page: 101 to 110
["white","whereas","withdraw","worth","working","w...
Page: 111 to 120
["wander","wound","weekly","wise","worried","wides...
Page: 121 to 130
["warm","warrior","wrist","walking","welcome","wei...
Page: 131 to 140
["wrong","worry","wake","way","withdrawal","wolf",...
Page: 141 to 150
["whoever","whip","workplace","waist","well-known"...
Page: 151 to 159
["well-being","weed","wheelchair","widow","warmth"...