0

How can i use $regex for matching any string contains '#'. Unlike other characters, # seems to be acting differently.

{"id" : {$regex : "what should go here"}}

does match strings that does not contains # as well. I was trying with .*\\#.* kind of regex.

aravindaM
  • 978
  • 8
  • 15

2 Answers2

1

Something like:

{"id" : {$regex : /^.*#.*/ }}

And for NOT contains:

{"id" : {$not : /^.*#.*/ }}

See the docs

Reference answer

Dabbas
  • 3,112
  • 7
  • 42
  • 75
1

Found out that it should be like this.

 {"id" : {$regex : /\#/i}}
aravindaM
  • 978
  • 8
  • 15
  • From the documentation, https://docs.mongodb.com/manual/reference/operator/query/regex/#perform-case-insensitive-regular-expression-match – aravindaM Dec 29 '17 at 08:38