admin管理员组文章数量:1357633
db.collection("accounts").findOne({Nickname: { $regex : new RegExp(player, "i") }}, function(err, result) { }
Is what I currently have, the problem is that I get everything that is a substring of the player variable. But I want only exact, albeit case-insensitive matches.
db.collection("accounts").findOne({Nickname: { $regex : new RegExp(player, "i") }}, function(err, result) { }
Is what I currently have, the problem is that I get everything that is a substring of the player variable. But I want only exact, albeit case-insensitive matches.
Share Improve this question asked Mar 16, 2018 at 21:23 teddyteddy 531 silver badge3 bronze badges2 Answers
Reset to default 5Your regex actually asks for a substring. To ask for an exact string:
new RegExp("^" + player + "$", "i")
(Keep everything else the same.)
The ^
matches the beginning of the input, the $
matches the end. This way, any substrings won't match.
Prefix player
with ^ and suffix with $, so that it matches the entire string
"^" + player + "$"
^ matches the start of the string
$ matches the end of the string
Reference for boundary regex characters
So with this expression you're saying "find me Nickname which starts and ends with this string" i.e. the entire string
本文标签: javascriptNodejsMongoDB findone with exact match but case insensitiveStack Overflow
版权声明:本文标题:javascript - Nodejs - MongoDB findone with exact match but case insensitive - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744079358a2587344.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论