2

I have a Mongo DB database with 80 or so documents. I want to only display documents from the 12th document to the 24th, or the 3rd to the 50th, etc.

What I mean by this, is since I have 80 documents, I only want to display a certain range of documents. For example, the number 12th document in the database all the way to the 24th.

How would I do this efficiently?

Thanks

Raghav Herugu
  • 545
  • 7
  • 19
  • Does this answer your question? [How to paginate with Mongoose in Node.js?](https://stackoverflow.com/questions/5539955/how-to-paginate-with-mongoose-in-node-js) Especially this answer: https://stackoverflow.com/a/59848661/3761628 – eol Aug 15 '20 at 19:44

1 Answers1

1

you can use skip and limit

const perPage = 12;

let page = 0;
// Select the 1st - 12th document
await this.SomeModel
    .find(query)
    .skip(page*perPage)
    .limit(perPage);

page = 1;
// Select the 13th - 24th document
await this.SomeModel
    .find(query)
    .skip(page*perPage)
    .limit(perPage);
Owl
  • 6,337
  • 3
  • 16
  • 30