-1

I want to search values having special characters such as ? ! $ / . @ > # in a document.

I want to search values such as ?test,

For example,

db.myCollection.find({ myKey : /.*?test.*/i }); 

OR

db.myCollection.find({ myKey : { '$regex' : '.*?test.*','$options' : 'i' });

What should I change here?

Karan Kris
  • 11
  • 2

1 Answers1

0

You need to double-escape the special character:

db.collection.find({
  myKey: {
    $regex: "\\?test",
    "$options": "i"
  }
})

https://mongoplayground.net/p/oaRlSJvFNG_

Daniel F
  • 13,684
  • 11
  • 87
  • 116