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

788
Views
MongoDB aggregation - $regexMatch on array field

I'm using Mongo's $regexMatch operator to find documents where at least part of a field matches the pattern, and this works fine for root-level fields. But how do I use it with array fields? I want to return a match if at least one of the array elements matches the pattern.

For example, let's say the collection has these two documents:

{
  "_id": ObjectId("5ff6335c1570ba63ca5ac21e"),
  "requirements": [
    {
      "description": "Bachelor of Science required for this blah blah blah",
      "code": "ABC"
    },
    {
      "description": "Also much experience in JavaScript blah",
      "code": "XYZ"
    }
  ]
},

{
  "_id": ObjectId("5ff6335b1570ba63ca5abefb"),
  "requirements": [
    {
      "description": "Master of Arts WANTED NOW!",
      "code": "TTT"
    },
    {
      "description": "5+ experience required in C++",
      "code": "QQQ"
    }
  ]
}

and something like this pipeline

db.Collection.aggregate([
  { $match:
     { $expr:
        { $regexMatch: { 
          input: '$requirements.description', 
          regex: /^.*?\bblah blah blah\b.*?$/im 
        } } 
     } 
  }
])

should return just the first document since its first element inside requirements matches on description containing "blah blah blah" ("Bachelor of Science required for this blah blah blah").

However this just throws me an error saying '$regexMatch needs input to be of type string'. And replacing it with $requirements[0].description doesn't work either.

So is there a way to regex match array fields in Mongo?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

$regexMatch allows only string input requirements having array It requires to iterate loop array values,

  • $reduce to iterate loop of description, check condition if expression match then return score otherwise return initial value
db.collection.aggregate([
  {
    $addFields: {
      score: {
        $reduce: {
          input: "$requirements.description",
          initialValue: 0,
          in: {
            $cond: [
              {
                $eq: [
                  {
                    $regexMatch: {
                      input: "$$this",
                      regex: "blah blah blah"
                    }
                  },
                  true
                ]
              },
              50,
              "$$value"
            ]
          }
        }
      }
    }
  }
])

Playground


If you want filter documents the simply try $regex in $match stage,

db.collection.aggregate([
  {
    $match: {
      "requirements.description": {
        $regex: "blah blah blah"
      }
    }
  }
])

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!