Querying for an exact match but case insensitive

1,333 views
Skip to first unread message

AGarcia

unread,
Nov 12, 2011, 9:02:47 AM11/12/11
to mongodb-csharp
What is the recommended way(s) of creating a search query for an exact
match on a field but making it case insensitive.

Maybe something like this?

Query.Matches("Username", new
BsonRegularExpression("/^" + username + "$", "i"))

Robert Stam

unread,
Nov 12, 2011, 9:09:43 AM11/12/11
to mongodb...@googlegroups.com
That will work, but keep in mind that it can't use any indexes.

If you need to use an index, your best best is to store the field twice, with the second copy being all lower case, and then using a regular EQ search against that.

    Query.EQ("UsernameLowerCase", username.ToLower())

and it will use an index on UsernameLowerCase if one exists.

Andres Garcia

unread,
Nov 12, 2011, 10:09:55 AM11/12/11
to mongodb...@googlegroups.com
Thanks...   I was expecting that answer...

Anoop Asokan

unread,
Sep 24, 2018, 9:55:53 AM9/24/18
to mongodb-csharp
Does it work if query contains '.' character?
Reply all
Reply to author
Forward
0 new messages