Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

164
Visualizações
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!

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda