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

328
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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