Skip to content

Instantly share code, notes, and snippets.

@rocktimsaikia
Last active January 14, 2021 15:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rocktimsaikia/6f0b0c53dab89aad3d485a5b84372fad to your computer and use it in GitHub Desktop.
Save rocktimsaikia/6f0b0c53dab89aad3d485a5b84372fad to your computer and use it in GitHub Desktop.
🔥 How to paginate in mongodb using mongoose

🔥 Here is how to paginate in MongoDB using mongoose :

// Here it is hardcoded for example but 
// generally the `pageNum` should come from request query param
const pageNum = 2;
const pageSize = 10;

// pagination formula
const paginate = (pageNum - 1) * pageSize; 

// Query
const data = await DataModel
  .find()
  .skip(paginate)
  .limit(pageSize)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment