MongoDB reverse regex

I have a document in MongoDB with regex attribute

    {
        _id: ObjectId("516023189732da20ce000004"),
        regex: /^(my|your)\s+regex$/
    }

and I need to get this document using db.col.find({regex: 'your regex'})and db.col.find({regex: 'my regex'}). In MySQL, I would have done: SELECT * FROM table WHERE 'my regex' REGEXP table.regex. How can I achieve this in MongoDB?

+3
source share
1 answer

You can use the $ where operator as follows:

db.col.find({$where: "\"my regex\".match(this.regex)"})
+6
source

Source: https://habr.com/ru/post/1652945/


All Articles