简体   繁体   中英

how to set limit,offset,skip in mongoose query

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

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

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?

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

write your code in 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 docs for more details

Try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM