如何一次查询得到count数量并且完成limte skip的分页任务
这样count得到的num始终为2
Movie
.count({title: new RegExp(q+'.*','i')},function(err,num){
totalPage = Math.ceil(num/2)
})
.find({title: new RegExp(q+'.*','i')})
.limit(2)
.skip(page * 2)
.exec(function(err,movies){
if(err){
console.log(err)
}
res.render('results',{
title: "结果列表页",
keyword: q,
movies:movies,
query: 'q=' + q,
totalPage:totalPage,
currentPage: (page+1)
})
})
})
2.这样count里面num数值对了,但是查询了两次
Movie
.count({title: new RegExp(q+'.*','i')},function(err,num){
totalPage = Math.ceil(num/2)
})
.exec(function(){
Movie
.find({title: new RegExp(q+'.*','i')})
.limit(count)
.skip(index)
.exec(function(err,movies){
if(err){
console.log(err)
}
res.render('results',{
title: " 结果列表页",
keyword: q,
movies:movies,
query: 'q=' + q,
totalPage:totalPage,
currentPage: (page+1)
})
})
})
如何一次查询得到count数量并且完成limte skip的分页任务?
Unfortunately, there is no such method, you can only check it twice. Think about this problem from the root. These are two different queries with different execution plans. How can they be completed in one query?