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

157
Vistas
Is there an easy way to implement a multi-field filter in MongoDB which includes partial string matching?

I'm currently attempting to implement a multi-field filter on a MongoDB database consisting of issues with multiple fields. I'm learning from the book "Pro MERN Stack" by Vasan Subramanian, but his multi-field filter doesn't include filtering by a String value. I would like my String filter to filter on partial matches, not just exact matches. For example, if a title is "Hello World" I would like a query of "Hello" to match with this title. I've included my functional JS code which currently works only if the title input matches exactly with the title field of an issue in the backend.

I've tried prepending the title with a '/' and appending it with an '/i', and when I print the filter using console.log it outputs { title: '/Hello/i' }. However, if I just hardcode the line and remove the single quotes around the argument to .find() by typing the line as const issues = await db.collection('issues').find({title: /Hello/i }).toArray(); it works on partial matches. Is there some easy way to resolve this issue?

async function list(_, {
  status, effortMin, effortMax, title,
}) {
  const db = getDb();
  const filter = {};

  if (status) filter.status = status;

  if (title) filter.title = title;

  if (effortMin !== undefined || effortMax !== undefined) {
    filter.effort = {};
    if (effortMin !== undefined) filter.effort.$gte = effortMin;
    if (effortMax !== undefined) filter.effort.$lte = effortMax;
  }

  const issues = await db.collection('issues').find(filter).toArray();
  return issues;
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

MongoDB already provides a solution that enables advanced search functionality Atlas Search

The process requires you to create an index for your search queries, such that queries requiring advanced search functionality will have to reference them.

Creating Atlas Search Indexes

Since you are interested in partially matching fields with in your document, the autocomplete field is what you want to define when creating your index

{
  $search: {
    "index": "<index name>", // optional, defaults to "default"
    "autocomplete": {
      "query": "<search-string>",
      "path": "<field-to-search>",
      "tokenOrder": "any|sequential",
      "fuzzy": <options>
    }
  }
}

Pay attention the the fuzzy object as it allows precise control over how the search is matched.

fuzzy.maxEdits Maximum number of single-character edits required to match the specified search term. Value can be 1 or 2.

fuzzy.prefixLength Number of characters at the beginning of each term in the result that must exactly match.

AutoComplete Documentation

Having completed creating the index, you are going to need to make queries using the $search aggregation pipeline stage as follows

{
  $search: {
    "index": "<index-name>",
    "autocomplete": {
      "query": "<search-string>",
      "path": "<field-to-search>"
      
    }
  }
}

$search aggregation pipeline stage documentation

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