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

322
Vistas
MongoDB: Ist there a possiblity to match a field containing a regular expression against a string in a $match stage in an aggregation pipeline?

I am currently trying to optimize some queries by using aggregation pipelines. Now I want to have a $match stage that filters out most of the collection documents in order to speed up the following stages. The problem I am facing is that the documents in the collection do contain fields that represent regular expressions. I now want to match strings against these regular expressions (preferably in the first $match stage).

This is how my current solution looks. But I am not really happy with this in terms of readability and probably performance.

[
    {$match: {
    name: "docName"
        }
    },
    {$addFields: {
    fieldAMatches: {$regexMatch: {input: "ABC", regex: '$fieldA'
                }
            }
        }
    },
    {$match: {
    fieldAMatches: true
        }
    }
]

A sample dataset would look the following:

[
    {
        "_id": 1,
        "name": "docName",
        "fieldA": ".*"
    },
    {
        "_id": 2,
        "name": "docName",
        "fieldA": "AB.*"
    },
    {
        "_id": 3,
        "name": "docName",
        "fieldA": "AB.+"
    },
    {
        "_id": 4,
        "name": "docName",
        "fieldA": "BAC"
    }
]

The result contains the following documents:

[
    {
        "_id": 1,
        "name": "docName",
        "fieldA": ".*"
    },
    {
        "_id": 2,
        "name": "docName",
        "fieldA": "AB.*"
    },
    {
        "_id": 3,
        "name": "docName",
        "fieldA": "AB.+"
    },
]
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can use $expr do achieve this, from the docs:

$match takes a document that specifies the query conditions. The query syntax is identical to the read operation query syntax; i.e. $match does not accept raw aggregation expressions. Instead, use a $expr query expression to include aggregation expression in $match.

Meaning by using $expr we can include aggregation expression (and in our case $regexMatch) in $match stage like so:

db.collection.aggregate([
  {
    $match: {
      name: "docName",
      $expr: {
        $regexMatch: {
          input: "ABC",
          regex: "$fieldA"
        }
      }
    }
  }
])

Mongo Playground

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