Clement
Clement

Reputation: 4811

Scalable Express + Mongoose Sort and Limit Pagination

I'm trying to work out how I can paginate my data efficiently. If I have a url such as: example.com/items?page=5, how can I retrieve the correct records every single time (so that the same record doesn't show up on another page)? I thought to I'd have to first sort the DB records first, then use mongoose to do a range query such as the one below.

How does this fare when the amount of records scales rapidly? I'm worried that the sorting process will take far too long and bottleneck the process. Any advice?

Submission.find({
  /* First Case: Hour */
  created: { $lt: new Date(), $gt: new Date(year+','+month+','+day+','+hour+','+min+','+sec) } // Get results from start of current hour to current time.
  /* Second Case: Day */
  created: { $lt: new Date(), $gt: new Date(year+','+month+','+day) } // Get results from start of current day to current time.
  /* Third Case: Month */
  created: { $lt: new Date(), $gt: new Date(year+','+month) } // Get results from start of current month to current time.
  /* Fourth Case: Year */
  created: { $lt: new Date(), $gt: new Date(year) } // Get results from start of current year to current time.
})

Upvotes: 0

Views: 1337

Answers (1)

Sandeep Sharma
Sandeep Sharma

Reputation: 1885

You can work on following query:

var query = Model.find(conditions).sort(sort).skip(skip).limit(limit);

where

  1. condition will be say { age: { $gt: 20 } }
  2. sort will be say { name: 1}
  3. skip will be say 20
  4. limit will be say 10

then execute the following query to get the records

return query
        .exec()
        .then(function (cursor) { ...... });

Upvotes: 1

Related Questions