search

Home  >  Q&A  >  body text

node.js - count和limit共同使用时得到的是limit传入的数值?

如何一次查询得到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的分页任务?

天蓬老师天蓬老师2875 days ago405

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 16:01:13

    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?

    reply
    0
  • Cancelreply