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);
});
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
}
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
}
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);
});
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))
}
}
//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');
}