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

168
Views
How to search by a field in mongoose with multiple keywords and sort results by the order the keywords are searched

I have a Product collection in mongoDB and I use mongoose. What am trying to achieve is, to search products by name

I have to search for the term "Los Angeles Lakers"

const results = await Products.find({
        $or: [
          { name: { $regex: "Los", $options: "i" } },
          { name: { $regex: "Angeles", $options: "i" } },
          { name: { $regex: "Lakers", $options: "i" } },
        ],
      });

This works fine. But I want to sort the results like, the search results of "Los" first, results of "Angeles" then, and the results of "Lakers" after that.

Also is there a way to accompish the above task similar to this

const results = await Products.find({
    $or: [
      { name: { $regex: $or: ["Los", "Angeles", "Lakers"], $options: "i" } }
    ],
  });

This is my first stack overflow question so bare with me if this is long or not formatted right. Thanks in advance!

over 4 years ago · Santiago Trujillo
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!