Node.js MongoDB中的正则表达式与特殊字符不匹配

Node.js MongoDB中的正则表达式与特殊字符不匹配,node.js,regex,mongodb,special-characters,string-matching,Node.js,Regex,Mongodb,Special Characters,String Matching,我想搜索具有特殊字符的值,如?!$/。@>#在文档中 我想搜索值,例如?test 比如说, db.myCollection.find({ myKey : /.*?test.*/i }); 或 我应该在这里更改什么?您需要双重转义特殊字符: db.collection.find({ myKey: { $regex: "\\?test", "$options": "i" } }) 为什么会有反对票?这怎么了?嘿

我想搜索具有特殊字符的值,如
?!$/。@>#在文档中

我想搜索值,例如
?test

比如说,

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


我应该在这里更改什么?

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

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

为什么会有反对票?这怎么了?嘿,丹尼尔。除了“#”之外,它对任何东西都有效。有什么解决办法吗?(不确定落选的人是谁,但那不是我)
db.collection.find({
  myKey: {
    $regex: "\\?test",
    "$options": "i"
  }
})