我想检查电子邮件是否已经在我的MongoDB数据库中注册。
我有一个用户注册了test123@test.com
但是,如果我检查test.23@test.com
,它仍然与test123@test.com
的记录匹配
我尝试了以下查询
db.getCollection('users').find(
{"email":{ $regex: new RegExp(test.23@test.com, "i") }
})
db.getCollection('users').find(
{"email":{ $regex: new RegExp(/test.23@test.com/, "i") }
})
db.getCollection('users').find(
{"email":{ $regex: new RegExp(/^(test.23@test.com)$/, "i") }
})
db.getCollection('users').find(
{"email":{ $regex: new RegExp(/^(test.23@test\.com)$/, "i") }
})
如何搜索不区分大小写的完全匹配?
答案 0 :(得分:1)
您不需要进行正则表达式。自v3.4起,Mongodb支持collations
db.getCollection('users').find(
{"email":"test.23@test.com"}
).collation(
{locale:"en", strength: 1}
)
将匹配“ test.23@test.com”,“ TEST.23@TEST.COM”以及介于两者之间的任何内容。
作为将用户输入直接传递到正则表达式查询的附带说明,您有一天可能会丢失服务器。阅读灵感https://www.rexegg.com/regex-explosive-quantifiers.html如果决定继续使用正则表达式方法,则可以在从正则表达式语法转义括号,方括号,转义字符和其他特殊符号时转义点。