0

In my MEAN app, I have a controller, which serves the following api:

app.get('/mongodata', function(req, res){
        console.log('Request Successful');
        console.log('_parsedUrl.query:  ' + req._parsedUrl.query);

        var url_parts = requestUrl.parse(req.url, true);
        var query = url_parts.query;

        var dataVersion = req.query.dataVersion;

        MongoClient.connect(url, function (err, db) {
            if (err) {
                console.log('Unable to connect to the mongoDB server. Error:', err);
            } else {
                console.log('Connection established to', url);

                var collection = db.collection(query.collectionName);

                collection.find({DataVersion: query.dataVersion}).toArray(function(err, docs) {
                    //console.log(docs);
                    res.json(docs);
                    assert.equal(null, err);
                    db.close();
                });

            }
        });
    });

What is the proper technique / syntax to add pagination capability to this api?

Eugene Goldberg
  • 14,286
  • 20
  • 94
  • 167
  • Look [here](http://stackoverflow.com/questions/776448/pagination-in-a-rest-web-application) and [here](http://stackoverflow.com/questions/2056241/pagination-with-mongodb) for some pointers. As an aside, you're creating a new database connection for each request, which isn't necessary and will probably not yield the best performance. – robertklep Jun 03 '15 at 18:41
  • I'm just barely starting with the MEAN stack. Point well taken - I will look to learn how to establish and reuse a single common db session. thanks! – Eugene Goldberg Jun 03 '15 at 18:43

0 Answers0