Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (47.6k points)

I am writing a web app with Node.js and mongoose. How can I paginate the results I get from a .find() call? I would like a functionality comparable to "LIMIT 50,100" in SQL.

 

2 Answers

0 votes
by (106k points)

To paginate with Mongoose in Node.js you can use the below-mentioned code for that:-

MyModel.find(query, fields, { skip: 10, limit: 5 }, function(err, results) { ... });

0 votes
by (108k points)

You have to add the ‘limit’ and ‘sort’ property for finding the data from the MongoDB collection. To perform pagination, you have to combine a limit( ) and one filter pattern. Filter patterns can be createdOn date groups, like say, for instance, createdOnBefore, refer to the following code:

MyModel.find( { createdOn: { $lte: request.createdOnBefore } } )

.limit( 10 )

.sort( '-createdOn' )

Related questions

Browse Categories

...