Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

318
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!