skip to Main Content

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

2

Answers


  1. Replace this:

    sort({'_id': "-1"})
    

    With:

    sort({_id: -1})
    
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search