1

I am storing regex expressions in MongoDB and would like to use them for queries.

Documents structure:

{
    "name": "foo",
    "regex_expression": "^[^@ ]+@(bar\\.com)$"
}

I tried the following query but it doesn't work.

db.collection.find({$expr: {$regex: ["[email protected]", "$regex_expression]}}) 

Is it possible to do this kind of query?

1 Answer 1

1

You need the $regexMatch operator.

db.collection.find({
  $expr: {
    $regexMatch: {
      input: "[email protected]",
      regex: "$regex_expression"
    }
  }
})

Demo @ Mongo Playground

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.