Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8767797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:54:30+00:00 2026-06-13T16:54:30+00:00

I have REST based API in NodeJS and MongooseJS ODM. To GET the list

  • 0

I have REST based API in NodeJS and MongooseJS ODM. To GET the list of departments URL is http://localhost:25718/Department?SortBy=Name&SortType=asc(desc-for descending). And sorting code is

 var Query = Department.find();
 if(req.query.SortBy) {
        Query.sort(req.query.SortBy, 1);
        if(req.query.SortType) {
            if(req.query.SortType.toLowerCase() == 'desc') {
                Query.sort(req.query.SortBy, -1);
            }
        }
    }

This dose not works with MongooseJS 3.3.1 . Query.sort(req.query.SortBy) gives sorted in ascending order.But not getting descending in any way.
Please give a solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:54:31+00:00Added an answer on June 13, 2026 at 4:54 pm

    I wasn’t able to recreate the problem you were experiencing. I would say to post the information about the collection you are querying along with the indexes you have on it.

    I did rewrite your code to be easier to read:

    var Query = Department.find(),
        sortBy = req.query.SortBy,
        sortType = (req.query.SortType || 'asc').toLowerCase();
    
    if (sortBy && sortType === 'asc') {
        Query.sort(sortBy, 1);  
    } else if (sortBy && sortType === 'desc') {
        Query.sort(sortBy, -1);
    }
    

    Update (2012-10-30)

    I just realized that I had the incorrect syntax for the sort function. Please see corrected code.

    var Query = Department.find(),
        sortBy = req.query.SortBy,
        sortType = (req.query.SortType || 'asc').toLowerCase();
    
    if (sortBy && sortType === 'asc') {
        Query.sort(sortBy); // firstname
    } else if (sortBy && sortType === 'desc') {
        Query.sort('-'+sortBy); // -firstname
    }
    

    See http://mongoosejs.com/docs/api.html#query_Query-sort for sort argument syntax.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an REST API with Yii Framework based on: http://www.yiiframework.com/wiki/175/how-to-create-a-rest-api/ I would like
I've implemented a REST-based API (using Tonic, FWIW) so I have a central dispatch.php
I have deployed a simple REST based application in RAD. A simple URL is
I am building an application with a REST-based API and have come to the
All, We are using ASP.NET Web API where we have a REST based service
We have app with REST API. It is based on Spring, Spring security is
I'm implementing an API using authentication based on this article: http://www.thebuzzmedia.com/designing-a-secure-rest-api-without-oauth-authentication/ And this related
I have a backend system which i access via a REST based API and
I am using http://json.codeplex.com/ and I am talking to a Ruby based Rest API.
am new to Json so a little green. I have a Rest Based Service

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.