How to paginate with Mongoose in Node.js ?

How to paginate with Mongoose in Node.js ?

Asked on December 31, 2018 in MongoDB.
Add Comment


  • 3 Answer(s)

    For after that focusing on Mongoose API with the information provided by Rodolphe,I figured out this solution:

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

     

    Answered on December 31, 2018.
    Add Comment

    For Pagination using mongoose, express and jade.

    var perPage = 10
      , page = Math.max(0, req.param('page'))
    Event.find()
        .select('name')
        .limit(perPage)
        .skip(perPage * page)
        .sort({
            name: 'asc'
        })
       .exec(function(err, events) {
            Event.count().exec(function(err, count) {
              res.render('events', {
                   events: events,
                   page: page,
                   pages: count / perPage
              })
           })
        })
    
    Answered on December 31, 2018.
    Add Comment

    You  can try do this code:

    var query = Model.find().sort('mykey', 1).skip(2).limit(5)
    
    

    To run the query using exec

    query.exec(callback);
    
    Answered on December 31, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.