Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

mongoose pagination with total count Code Example

  let limit = parseInt(req.body.limit) || 10
  let page = parseInt(req.body.page) - 1 || 0
  var query = {};
  let users = await Users.find({}).sort({ createdAt: -1 }).skip(limit * page).limit(limit).select("fields you want to select separated by space")
  const count = await Users.countDocuments(query);
  let obj = {
    Users: users,
    total: count,
    limit: limit,
    page: page + 1
  }
 
PREVIOUS NEXT
Tagged: #mongoose #pagination #total #count #Code #Example
ADD COMMENT
Topic
Name
9+1 =