Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

161
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda