node.js - count和limit共同使用时得到的是limit传入的数值?
天蓬老师
天蓬老师 2017-04-17 15:59:13
0
1
381

如何一次查询得到count数量并且完成limte skip的分页任务

  1. 这样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的分页任务?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(1)
Ty80

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?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template