我正在用Node.js和mongoose写一个web应用程序。如何对我从.find()调用得到的结果进行分页?我想要一个功能可比的“限制50,100”在SQL。
当前回答
你可以使用一个叫Mongoose Paginate的小包,让它更容易。
$ npm install mongoose-paginate
在你的路由或控制器后,只需添加:
/**
* querying for `all` {} items in `MyModel`
* paginating by second page, 10 items per page (10 results, page 2)
**/
MyModel.paginate({}, 2, 10, function(error, pageCount, paginatedResults) {
if (error) {
console.error(error);
} else {
console.log('Pages:', pageCount);
console.log(paginatedResults);
}
}
其他回答
const ITEMS_PER_PAGE = 2;
exports.getProducts = (req, res, next) => {
// + will turn the string to a number
const page = +req.query.page || 1;
let totalItems;
//Product model
Product.find()
.countDocuments()
.then((numProducts) => {
totalItems = numProducts;
return Product.find()
//If query param is 3, since ITEMS_PER_PAGE = 2, we skip 2*2 items
// we show only 5th and 6th item
.skip((page - 1) * ITEMS_PER_PAGE)
.limit(ITEMS_PER_PAGE);
})
.then((products) => {
res.render("shop/products", {
// maybe sending the products object to templating engine
});
})
.catch((err) => {
const error = new Error(err);
error.httpStatusCode = 500;
// if you are set express error handler, use this
// when we call next() with an argument passed in, we let express know, we skip all other middlewares, we move to error handling middleware
return next(error);
});
};
**//localhost:3000/asanas/?pageNo=1&size=3**
//requiring asanas model
const asanas = require("../models/asanas");
const fetchAllAsanasDao = () => {
return new Promise((resolve, reject) => {
var pageNo = parseInt(req.query.pageNo);
var size = parseInt(req.query.size);
var query = {};
if (pageNo < 0 || pageNo === 0) {
response = {
"error": true,
"message": "invalid page number, should start with 1"
};
return res.json(response);
}
query.skip = size * (pageNo - 1);
query.limit = size;
asanas
.find(pageNo , size , query)
.then((asanasResult) => {
resolve(asanasResult);
})
.catch((error) => {
reject(error);
});
});
}
最好的方法(IMO)是在有限的集合或文档中使用跳过和限制BUT。
要在有限的文档中进行查询,可以使用特定的索引,例如DATE类型字段上的索引。见下图
let page = ctx.request.body.page || 1
let size = ctx.request.body.size || 10
let DATE_FROM = ctx.request.body.date_from
let DATE_TO = ctx.request.body.date_to
var start = (parseInt(page) - 1) * parseInt(size)
let result = await Model.find({ created_at: { $lte: DATE_FROM, $gte: DATE_TO } })
.sort({ _id: -1 })
.select('<fields>')
.skip( start )
.limit( size )
.exec(callback)
这是我在代码中做的
var paginate = 20;
var page = pageNumber;
MySchema.find({}).sort('mykey', 1).skip((pageNumber-1)*paginate).limit(paginate)
.exec(function(err, result) {
// Write some stuff here
});
我就是这么做的。
也可以用async/await实现结果。
下面的代码示例使用hapi v17和mongoose v5的异步处理程序
{
method: 'GET',
path: '/api/v1/paintings',
config: {
description: 'Get all the paintings',
tags: ['api', 'v1', 'all paintings']
},
handler: async (request, reply) => {
/*
* Grab the querystring parameters
* page and limit to handle our pagination
*/
var pageOptions = {
page: parseInt(request.query.page) - 1 || 0,
limit: parseInt(request.query.limit) || 10
}
/*
* Apply our sort and limit
*/
try {
return await Painting.find()
.sort({dateCreated: 1, dateModified: -1})
.skip(pageOptions.page * pageOptions.limit)
.limit(pageOptions.limit)
.exec();
} catch(err) {
return err;
}
}
}
推荐文章
- 错误:无法找到模块“webpack”
- 在node.js中使用async / await文件系统
- NodeJS -用NPM安装错误
- 如何为本地安装npm包设置自定义位置?
- 回调函数来处理管道的完成
- Express函数中的“res”和“req”参数是什么?
- node.js TypeError:路径必须是绝对路径或指定根路径到res.sendFile[解析JSON失败]
- Passport.js -错误:序列化用户到会话失败
- Node.js vs .Net性能
- 从电子应用程序中删除菜单栏
- 如何用node.js实现一个安全的REST API
- 如何处理Node.js中的循环依赖
- 在猫鼬,我如何排序的日期?(node . js)
- 如何在Node.js内进行远程REST调用?旋度吗?
- 将映像存储在MongoDB数据库中