Home > Software engineering >  Mongoose query to sort, skip and limit
Mongoose query to sort, skip and limit

Time:09-04

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

CodePudding user response:

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

CodePudding user response:

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

  • Related