Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

mongoose pagination

const pageOptions = {
    page: parseInt(req.query.page, 10) || 0,
    limit: parseInt(req.query.limit, 10) || 10
}

sexyModel.find()
    .skip(pageOptions.page * pageOptions.limit)
    .limit(pageOptions.limit)
    .exec(function (err, doc) {
        if(err) { res.status(500).json(err); return; };
        res.status(200).json(doc);
    });
Comment

mongoose pagination

  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
  }
Comment

mongoose pagination

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
  }
Comment

mongoose pagination

const pageOptions = {
    page: parseInt(req.query.page, 10) || 0,
    limit: parseInt(req.query.limit, 10) || 10
}

sexyModel.find()
    .skip(pageOptions.page * pageOptions.limit)
    .limit(pageOptions.limit)
    .exec(function (err, doc) {
        if(err) { res.status(500).json(err); return; };
        res.status(200).json(doc);
    });
Comment

mongoose pagination

  async getAllUsers(query: DTOUsersPagination): Promise<APIResponse> {
    try {
      if (!query.hasOwnProperty('limit') && !query.hasOwnProperty('offset') && !query.hasOwnProperty('sort')) {
        query.limit = 10
        query.offset = 0
        query.sort = 'asc' ? 1 : -1
      }

      let getAllUsers: IUsers[] = []

      if (query.hasOwnProperty('filter') && JSON.parse(query.filter as any) == true) {
        const schemaFields: string[] = Object.keys(this.users.model.schema['paths'])
        const groupQuery: string[] = Object.keys(query)

        const getKey: any = schemaFields.find((val: string) => groupQuery.indexOf(val) !== -1 && val)
        const getValue: any = schemaFields.find((val: string) => query[val] && val)

        if (!getKey && !getValue) throw apiResponse(status.BAD_REQUEST, 'filter schema field not valid')

        getAllUsers = await this.users.model
          .find({}, { __v: 0 })
          .where({ [getKey]: query[getValue] })
          .limit(query.limit)
          .skip(query.offset)
          .sort({ _id: query.sort })
      } else {
        getAllUsers = await this.users.model.find({}, { __v: 0 }).limit(query.limit).skip(query.offset).sort({ _id: query.sort })
      }

      const currentPage: number = 1
      const countData: number = await this.users.model.count()
      const totalPage: number = Math.ceil(countData / query.page)

      const pagination: Record<string, any> = {
        count: countData,
        limit: +query.limit,
        offset: +query.offset,
        currentPage: +query.offset > 0 ? currentPage + 1 : currentPage,
        perPage: +query.page,
        totalPage: totalPage
      }

      return Promise.resolve(apiResponse(status.OK, 'Users already to use', getAllUsers, pagination))
    } catch (e: any) {
      return Promise.reject(apiResponse(e.stat_code || status.BAD_REQUEST, e.stat_message || e.message))
    }
  }
Comment

pagination in mongoose express node.js

//Pagination
    //page=2,limit=10
    let page = req.query.page * 1 || 1;
    let limit = req.query.limit * 1 || 100;
    let skip = (page - 1) * limit;
    query = query.skip(skip).limit(limit);
    if (req.query.page) {
      const numTours = await Tour.countDocuments();
      if (skip >= numTours) throw new Error('This page does not exists');
    }
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to lose overflow in js without hidden 
Javascript :: check variable value and do something 
Javascript :: diferença entre let e var 
Javascript :: set state 
Javascript :: react footer component 
Javascript :: alert library css and js 
Javascript :: sequelize 
Javascript :: what is form data in javascript 
Javascript :: react native position 
Javascript :: javascript add field to array 
Javascript :: javascript destructing 
Javascript :: if condition javascript 
Javascript :: click tester 
Javascript :: javascript symbols 
Javascript :: map react 
Javascript :: json web token flask 
Javascript :: dynamodb count items node 
Javascript :: find number in array js 
Javascript :: what is next.js 
Javascript :: js to find min value in an array 
Javascript :: empty javascript 
Javascript :: write !important in react 
Javascript :: js select all 
Javascript :: $(...).Datatables is not a function 
Javascript :: clean-webpack-plugin clearing dist folder 
Javascript :: window frames javascript 
Javascript :: classes in javascript mdn 
Javascript :: how to calculate first monday of the month in js 
Javascript :: dynamic classname react 
Javascript :: array iterator javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =