I want to search values having special characters such as " $ / . @ > "
in a document.
Lets consider, I've myKey with values like "test$australia", "test$austria", "test$belgium", "green.africa".
I want to search values with '.*$aus.*',
For example,
db.myCollection.find({ myKey : /.*$aus.*/i });
OR
db.myCollection.find({ myKey : { '$regex' : '.*$aus.*','$options' : 'i' });
Above queries dont work , how should I form query? I'm using MongoDB 2.4.1.
You have to escape $
by \
:
db.myCollection.find({ myKey : /.*\$aus.*/i }); // OR db.myCollection.find({myKey: { $regex: '.*\\$aus.*', $options: 'i'}})
You can use this:
db.myCollection.find({myKey:{ $regex:new RegExp('^' + 'test$australia'.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + '$', 'i')}})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With