Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo reverse regex [duplicate]

In SQL it is possible to run a query similar to

SELECT result FROM table WHERE 'abc-def-ghi' LIKE col1

on a table like this:

col1      | result
abc-%     | 1
abc-d%    | 2
as%       | 3
...       | ...

and get a result set with 1 and 2.

Problem: how do I achieve same effect in mongodb? I can run regex to match against fields but is there a way that the fields could be match agains supplied data?

like image 248
karka91 Avatar asked Sep 04 '26 16:09

karka91


1 Answers

You can use the $where operator, e.g.:

db.col.find({$where: "\"abc\".match(this.col1)"})

Although MongoDB documentation doesn't recommend using $where, it is the only possibility as far as I know.

(This question is probably a duplicate. See MongoDB reverse regex)

like image 161
fgalan Avatar answered Sep 06 '26 14:09

fgalan