0 votes
1 view
in SQL by (47.8k 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 (107k 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 (78.7k 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

Welcome to Intellipaat Community. Get your technical queries answered by top developers !