2

I'm trying to make an easy pagination for a REST service with MongoDB and Mongoose. Here's how I'm doin it right now.

As you can see I'm sending 0 on 'next' variable, but I just want to send zero when there are no more results. How could I get when results are over and there are no more to get?

   var user = User.find(
    {},
    {},
    {skip: skip, limit: limit},
    function(err, docs) {
        if (!err) {
            res.json({users: docs, next: 0} );
        } else {
            throw err;
        }
    });

Thanks!

PS: I'm using node.js and express.

Javier Manzano
  • 4,661
  • 14
  • 51
  • 82

2 Answers2

0

Try this:

if ( !docs ) {
  res.json({users: [], next: 0});
}
chovy
  • 59,357
  • 43
  • 187
  • 234
0

if i got your question right you would like to send the ammount of remaining documents for the next query, if so you could query with limit * 2 but only return 'limit' documents, substract limit from docs.length and you will get the ammount of remaining documents for the next query.

there may be a better solution with a more adanvced query.

supernova
  • 3,606
  • 2
  • 19
  • 34