我正在用Node.js和mongoose写一个web应用程序。如何对我从.find()调用得到的结果进行分页?我想要一个功能可比的“限制50,100”在SQL。
当前回答
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);
});
};
其他回答
在用Rodolphe提供的信息仔细研究了Mongoose API后,我想出了这个解决方案:
MyModel.find(query, fields, { skip: 10, limit: 5 }, function(err, results) { ... });
app.get("/:page",(req,res)=>{
post.find({}).then((data)=>{
let per_page = 5;
let num_page = Number(req.params.page);
let max_pages = Math.ceil(data.length/per_page);
if(num_page == 0 || num_page > max_pages){
res.render('404');
}else{
let starting = per_page*(num_page-1)
let ending = per_page+starting
res.render('posts', {posts:data.slice(starting,ending), pages: max_pages, current_page: num_page});
}
});
});
最简单的分页插件。
https://www.npmjs.com/package/mongoose-paginate-v2
添加插件到一个模式,然后使用模型paginate方法:
var mongoose = require('mongoose');
var mongoosePaginate = require('mongoose-paginate-v2');
var mySchema = new mongoose.Schema({
/* your schema definition */
});
mySchema.plugin(mongoosePaginate);
var myModel = mongoose.model('SampleModel', mySchema);
myModel.paginate().then({}) // Usage
let page,limit,skip,lastPage, query;
page = req.params.page *1 || 1; //This is the page,fetch from the server
limit = req.params.limit * 1 || 1; // This is the limit ,it also fetch from the server
skip = (page - 1) * limit; // Number of skip document
lastPage = page * limit; //last index
counts = await userModel.countDocuments() //Number of document in the collection
query = query.skip(skip).limit(limit) //current page
const paginate = {}
//For previous page
if(skip > 0) {
paginate.prev = {
page: page - 1,
limit: limit
}
//For next page
if(lastPage < counts) {
paginate.next = {
page: page + 1,
limit: limit
}
results = await query //Here is the final results of the query.
也可以用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;
}
}
}
推荐文章
- Node.js -以平台不可知的方式查找主目录
- 为什么Node.js是单线程的?
- 比较mongoose _id和字符串
- .js和.mjs文件有什么区别?
- 如何修复npm UNMET PEER DEPENDENCY警告?
- 用ReactJS在localStorage中存储JWT安全吗?
- 用于XML解析的最佳节点模块
- Firebase Firestore“引用”数据类型的好处是什么?
- 如何让一个node.js应用程序永久运行?
- MongoDB:如何将多个集合中的数据合并为一个?
- 在npm install grunt——save-dev中-save-dev是什么意思
- 在Express.js中使用next()将变量传递给下一个中间件
- 如何Polyfill节点核心模块在webpack 5
- Router.use()需要中间件函数,但是得到了一个Object
- MongoDB - admin用户未授权