mongoose查询列表分页终极写发
/**
* pageNum 默认从0开始 pageSize 10页面
*/
Router.post('/goodsList', (req, res) => {
const {pageNum = 1, pageSize = 10, goodsName = '', goodsPrice = ''} = req.body
Goods.count({ // 获取数据条数
$or: [
{goodsName: {'$regex': goodsName, $options: '$i'}}
]
}, (err, count) => { //查询出结果返回
Goods.find({
$or: [
{goodsName: {'$regex': goodsName, $options: '$i'}}
]
})
.skip((pageNum - 1) * pageSize)
.limit(pageSize)
.sort({'_id': -1})
.exec((err, doc) => {
try {
if (!err && doc) {
return res.json({code: 0, totalCount: count, msg: '列表获取成功', data: doc})
}
res.json({code: 1, msg: '后端出错'})
} catch (e) {
res.json({code: 1, msg: '后端出错'})
}
})
})
})
网友评论