Searching string with special characters in MongoDB document

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')}})

Leave a Comment