简体   繁体   English

Java - MongoDB 不区分大小写,不检查完全匹配

[英]Java - MongoDB case insensitive not checking for exact match

I'm trying to have case insensitive queries but by doing this, it's possible to find users by inputting parts of their username.我正在尝试进行不区分大小写的查询,但通过这样做,可以通过输入用户名的一部分来找到用户。 ie a username types in "f" and there is a user with the username "foo" it will return foo's document.即用户名键入“f”,并且有一个用户名为“foo”,它将返回 foo 的文档。

I'm using this to check for case insensitive matches我用它来检查不区分大小写的匹配

private DBObject getQuery(String where, String whereValue)
{
    return new BasicDBObject(where, Pattern.compile(whereValue, Pattern.CASE_INSENSITIVE));
}

How can I check for exact matches but ignore case?如何检查完全匹配但忽略大小写?

This is really all about your regex pattern you are using.这实际上是关于您正在使用的正则表达式模式。 For "foo" and only "foo" anywhere in the string, use this:对于字符串中任何位置的“foo”和“foo”,请使用:

/\bfoo\b/i

Or specifically at the "start" of the string:或者特别是在字符串的“开始”处:

/^foo\b/i

Without the specifics you will not get a "specific" match.如果没有细节,您将无法获得“特定”匹配。

MongoDB uses the pcre library ( or at least compatible ) so use expressions that match that constraint. MongoDB 使用pcre库(或至少兼容),因此使用匹配该约束的表达式。

Of course that does include the "i" option for case insensitive as you asked.当然,正如您所要求的那样,这确实包括不区分大小写的“i”选项。 So this will match the "words" for "foo" and "FOO" as well as other possible case variations.因此,这将匹配“foo”和“FOO”的“words”以及其他可能的大小写变化。

Also note, that when using MongoDB regex based queries, unless the string is "anchored" with the caret "^" to the start of the string, then the search is done over all documents in the collection, and not just only those that contain "foo" in some form somewhere.另请注意,当使用基于 MongoDB 正则表达式的查询时,除非字符串“锚定”在字符串的开头并带有插入符号“^”,否则将在集合中的所有文档中进行搜索,而不仅仅是那些包含某处某种形式的“foo”。

Only the caret "^" can make use of an index, but then again the "case insensitive" option also blows this away.只有插入符号“^”可以使用索引,但“不区分大小写”选项也将其消除。

For efficient searches, include a string in your document with "normalized" case, and hopefully be looking from the beginning of the string only.为了高效搜索,请在您的文档中包含一个带有“规范化”大小写的字符串,并希望仅从字符串的开头查看。

Start your regular expression with ^ character and end with $ character and it will works fine.以 ^ 字符开始您的正则表达式并以 $ 字符结束,它会正常工作。

private DBObject getQuery(String where, String whereValue)
{
    return new BasicDBObject("^" + where + "$", Pattern.compile(whereValue, Pattern.CASE_INSENSITIVE));
}

In given above answer one thing is mistaken.在上面给出的答案中,一件事是错误的。 regex is applied on key rather than value.正则表达式应用于键而不是值。 to rectify it we should apply regex on value rather than regex like code given below.为了纠正它,我们应该对值应用正则表达式,而不是像下面给出的代码那样的正则表达式。

private DBObject getQuery(String where, String whereValue)
{
return new BasicDBObject(where, Pattern.compile("^" + whereValue + "$", Pattern.CASE_INSENSITIVE));
}

Now it will correctly make case insensitive search.现在它将正确进行不区分大小写的搜索。 Enjoy Dear :)享受亲爱的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM