javascript - Nodejs - MongoDB findone with exact match but case insensitive - Stack Overflow

db.collection("accounts").findOne({Nickname: { $regex : new RegExp(player, "i") }},

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 badges
Add a ment  | 

2 Answers 2

Reset to default 5

Your 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744079358a4554998.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信