Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

275
Views
How do sort in mongo based on objects in url query string?
I want to sort a mongo collection on base of following query in URL string from a node application.

http://localhost:5000/v1/bid/sort_bid?sort={"_id": -1,"enquiry_no": 1}

how do I pass an object in following code?

const getSortedBidList = catchAsync(async (req, res) => {
let { sort } = req?.query;
    console.log('Query String', sort);
    let object = new Object();
    object[sort] === 1 ? 'ASC' : 'DESC';

let result = await Bid
        .find({})
        .sort(object)
        .limit(limit)
        .skip(offset);

} In the sort method I am passing object of query string, but it didn't work. Please help to sort this out. Thank You

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try the following:

http://localhost:5000/v1/bid/sort_bid?sort[_id]=-1&sort[enquiry_no]=1

Learn more about how express parses query strings from here.

I recommend that you use npm qs package to parse the objects on your frontend into query strings programatically:

import qs from 'qs';

async function sendRequestToBackend(queryAsObject) {
  const stringifiedQuery = qs.stringify(queryAsObject);
  await sendRequest(`http://localhost:5000/v1/bid/sort_bid?${stringifiedQuery}`)
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!