Home > OS >  How to get only 10 documents at a time in mongodb?
How to get only 10 documents at a time in mongodb?

Time:06-10

const result = student.find();

In mongodb database i require to get 10 documents on every buttion click and next time on click i need to get net 10 documents in mongodb.

CodePudding user response:

You can pass limit method after student.find(). For example if you want to limit 10 documents it'll look like this const result = student.find().limit(10)

CodePudding user response:

Follow below steps - Step 1 - Pass limit and skip value in api query like - baseurl/api/v1/apiendpoint?limit=10&skip=0

Step 2 - Fetch limit and skip value from url const limit = req.query.limit; const skip = req.query.skip;

step 3 - const result = student.find().skip(skip).limit(limit);

Note - Limit and skip value will be change as per your click.

CodePudding user response:

You can specifify a limit and an offset for your query by using skip() and limit():

const page = 1;
const itemsPerPage = 10;

const result = await student.find().skip(page * itemsPerPage).limit(itemsPerPage);
  • Related