Home:ALL Converter>MongoDB reverse regex

MongoDB reverse regex

Ask Time:2013-04-12T16:53:42         Author:Giovanni Lovato

Json Formatter

I have a document in MongoDB with a regex attribute

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

and I need to retrieve this document with something like db.col.find({regex: 'your regex'}) and db.col.find({regex: 'my regex'}). In MySQL I'd do: SELECT * FROM table WHERE 'my regex' REGEXP table.regex. How can I achieve this in MongoDB?

Author:Giovanni Lovato,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/15966991/mongodb-reverse-regex
fgalan :

You can use the $where operator in the following fashion:\n\ndb.col.find({$where: \"\\\"my regex\\\".match(this.regex)\"})\n",
2013-04-13T15:36:32
yy