简体   繁体   中英

Mongoose query to sort, skip and limit

Here is my mongoose code which does sorting And pagination. Here when i am removing sorting or adding other type of sort no result is coming. And i have 3000 documents in db

  res = await Question.find(filters)
  .sort({'_id': "-1"})
  .skip(args.skip ? parseInt(args.skip) : 0)
  .limit(args.limit ? parseInt(args.limit) : 100)
  .exec();

Please take a look

Replace this => sort({'_id': "-1"}) with => sort({_id: -1})

The .sort() command accept as Objects or strings as parameters.

On the Object syntax the values allowed are asc , desc , ascending , descending , 1 , and -1

  .sort({_id: -1})

string syntax

  .sort('-_id').

Note: You don't need the .exec() part when you are using promises

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