node.js 如何在猫鼬查询中设置限制、偏移、跳过

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39719021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:35:05  来源:igfitidea点击:

how to set limit,offset,skip in mongoose query

node.jsexpressmongoose

提问by coder

I am using express,mongoose,node.so i have checked this query regarding to skip and limit of documents.

我正在使用 express、mongoose、node.so 我检查了这个关于跳过和限制文档的查询。

   Model.find({},null,{limit:2,skip:20},function(err,data){
           //console.log(data)
      })

回答by Neeraj Prajapati

not sure but I think it's help full for you.

不确定,但我认为这对您很有帮助。

var perPage = 10
  , page = Math.max(0, req.param('page'))

Event.find()
    .select('name')
    .skip(perPage * page)
    .limit(perPage)
    .sort({
        name: 'asc'
    })
    .exec(function(err, events) {
        Event.count().exec(function(err, count) {
            res.render('events', {
                events: events,
                page: page,
                pages: count / perPage
            })
        })
    })

or see

或看

How to paginate with Mongoose in Node.js?

如何在 Node.js 中使用 Mongoose 进行分页?

回答by abdulbarik

var query = Model.find({}).skip(2).limit(5)
query.exec(callback);

write your code in callback

写你的代码 callback

query.exec(function(err,data){
   //console.log(data)
});

You can also do some more query like this

你也可以做更多这样的查询

Model
.where('field1').gte(25)
.where().in([])
.select('field1', 'field2', 'field13')
.skip(20)
.limit(10)
.asc('field1')
.exec(callback);

You can refer docsfor more details

您可以参考文档了解更多详情

回答by Kishore Padmanaban

Try this

尝试这个

Model.find({},null,{limit:2,skip:20}).then(function(data){
       //console.log(data)
  }).catch(next);