Searching string with special characters in MongoDB document

后端 未结 7 1912
清歌不尽
清歌不尽 2020-12-05 23:57

I want to search values having special characters such as \" $ / . @ > \" in a document.

Lets consider, I\'ve myKey with values like \"test$aus

7条回答
  •  一生所求
    2020-12-06 00:22

    You have to escape $ by \:

    db.myCollection.find({ myKey : /.*\$aus.*/i }); 
    // OR
    db.myCollection.find({myKey: { $regex: '.*\\$aus.*', $options: 'i'}})
    

提交回复
热议问题