提问者:小点点

MongoDB中的正则表达式不匹配特殊字符[重复]


我想搜索具有特殊字符的值,如? ! $/。 @>;

我想搜索诸如?test之类的值,

例如,

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

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

在这里我该换什么?


共1个答案

匿名用户

您需要对特殊字符进行双重转义:

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

https://mongoplayground.net/p/oarlsjvfng_