Paging Results

A very common method of reducing the number of documents returned is paging. Paging involves specifying a number of documents to skip in the matching set as well as a limit on the documents returned. Then the skip value is incremented each time by the amount returned the previous time.

To implement paging on a set of documents, you need to implement the limit and skip options on the options object. The skip option specifies a number of documents to skip before returning documents. By moving the skip value each time you get another set of documents, you can effectively page through the data set. Also, you should always include a sort option when paging data to ensure that the order is always the same. For example, the following statements find documents 1–10, then 11–20, and then 21–30:

collection.find({},{sort:[['_id':1]], skip:0, limit10},
                function(err, cursor){});
collection.find({},{sort:[['_id':1]], skip:10, limit10}, function(err, cursor){});
collection.find({},{sort:[['_id':1]], skip:20, limit10}, function(err, cursor){});

Listing 15.5 shows how to use limit and skip to page through a specific set of documents. A new find() request is implemented each time, and this more closely mimics what would happen when handling paging requests from a webpage. Figure 15.5 shows the output of Listing 15.5. Notice that words are retrieved 10 at a time.

Image

Figure 15.5 Paging the results on a specific set of documents in MongoDB by using the skip, limit, and sort options.


Warning

If the data on a system changes in such a way that it affects the results of a query, a skip may miss some items or include items again in a subsequent page request.


Listing 15.5 doc_paging.js: Paging results from a specific set of documents in a MongoDB collection


01 var util = require('util'),
02 var MongoClient = require('mongodb').MongoClient;
03 var myDB;
04 MongoClient.connect("mongodb://localhost/", function(err, db) {
05   myDB = db.db("words");
06   myDB.collection("word_stats", function(err, collection){
07     pagedResults(err, collection, 0, 10);
08   });
09 });
10 function displayWords(msg, cursor, pretty){
11   cursor.toArray(function(err, itemArr){
12     console.log(" "+msg);
13     var wordList = [];
14     for(var i=0; i<itemArr.length; i++){
15       wordList.push(itemArr[i].word);
16     }
17     console.log(JSON.stringify(wordList, null, pretty));
18   });
19 }
20 function pagedResults(err, words, startIndex, pageSize){
21   words.find({first:'v'},
22              {limit:pageSize, skip:startIndex, sort:[['word',1]]},
23              function(err, cursor){
24     cursor.count(true, function(err, cursorCount){
25       displayWords("Page Starting at " + startIndex, cursor);
26       if (cursorCount === pageSize){
27         pagedResults(err, words, startIndex+pageSize, pageSize);
28       } else {
29         myDB.close();
30       }
31     });
32   });
33 }


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.100.212