1

I have a node application that works with mongodb (native driver). I have docs with a field "parent" for example, "parent":"root/test/dir/". This is how I try to find all the docs that the "parent" field contains for example "root/test". db.collection.find({"parent": new Regexp("root/test")}) it doesn't work.

h. Oo
  • 361
  • 1
  • 2
  • 10

2 Answers2

2

As @Blobonat points out, you can use the back slash character to over-ride the special meaning of the following character in a regular expression.

new Regexp("root\/test")

The MDN is a good resource for this:

A backslash that precedes a special character indicates that the next character is not special and should be interpreted literally.

Likewise the inverse holds true.

A backslash that precedes a non-special character indicates that the next character is special and is not to be interpreted literally.

You can find more information here

There is also a great set of example here from a question on SO for working with URLs and RegExp

Community
  • 1
  • 1
alex
  • 5,467
  • 4
  • 33
  • 43
  • 1
    For extra fun strings like `this\that` become `this\\that` because you have to backslash the backslash, which leads to some hard to real regex expressions like `\\/([^;]+);version=` – Michael Shopsin Aug 02 '16 at 14:18
0

The problem was with mongodb driver. The remove function didn't work for me. What did work is the deleteMany. My regexp was fine, I loged it out and as the answers above sad it was /\root/\test'. new Regexp works great.

h. Oo
  • 361
  • 1
  • 2
  • 10