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

240
Vistas
Mongoose find or multiple columns not working

If keyword maches in category.title then it is giving result otherwise it is not matching with any other fields. Can somebody tell me what is the issue here? Also tried NewsModel.find().or(filters) without $or key, still not working.

  let filters = {};

  if (keyword) {
    filters = {
      $or: [
        {
          title: keyword,
        },
        {
          shortDescription: keyword,
        },
        {
          detailDescription: keyword,
        },
        {
          "category.title": keyword,
        },
      ],
    };
  }

  const response = await NewsModel.find(filters);

mongoose:6.3.0

Update: DB collection added

 {
        "_id": "625b83518b1eaedc58a74196",
        "title": "Moon Knight",
        "shortDescriptipn": "Embrace the chaos.",
        "detailDescription": "When Steven Grant, a mild-mannered gift-shop employee...",
        "category": [
            {
                "_id": "625b7dfd00e7f082f6d8d9fb",
                "title": "Entertainment"
            }
        ],
        "createdAt": "2022-04-17T03:02:41.353Z",
        "updatedAt": "2022-04-17T03:02:41.353Z",
        "__v": 0
    }

If I am searching for Entertainment, I get result but if I am searching for Moon Knight, I get empty array.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use this code to make it work, also you have misspelled shortDescriptipn in the database.

What i did is, i used $or keyword with regex search so you dont need to write full string, if it contains that part of string, it will return the document and wont be a strict full match case, also i have added option i which will make the search case insensitive.

The query will be like:

db.collection.find({
  $or: [
    {
      title: {
        $regex: keyword,
        $options: "i"
      },
      
    },
    {
      shortDescriptipn: {
        $regex: keyword,
        $options: "i"
      },
      
    },
    {
      detailDescription: {
        $regex: keyword,
        $options: "i"
      },
      
    },
    {
      "category.title": {
        $regex: keyword,
        $options: "i"
      }, 
    },
  ],
})

You can also check the query here : https://mongoplayground.net/p/7zuhbaNTWJY

Let me know, if you have any doubts in comments.

about 4 years ago · Juan Pablo Isaza Denunciar
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