0

I'd like to set a mongoDb request ignoring capitals

If I only have one user with test username

db.users.find(
    { username: "test" }
)

will return something I'd like to return the profile test with

db.users.find(
    { username: "Test" }
)

or

db.users.find(
    { username: "tEsT" }
)

But I don"t know I to ignore capitals

same problem if I have a username Toby I'd like to find with

db.users.find(
    { username: "toby" }
)

Thanks

Ajouve
  • 9,735
  • 26
  • 90
  • 137
  • Seems to be a duplicate, rich answers could be found there: http://stackoverflow.com/questions/1863399/mongodb-is-it-possible-to-make-a-case-insensitive-query – udalmik Nov 20 '12 at 14:02

1 Answers1

2

You can use regex to ignore capitals like:

db.users.find({username: /test/i})

Whereby the i option state case insensitive. This however is not very index friendly.

A better option is to do this client side. Normalise your case so that everything is either lower case or upper case. That way you can just test as normal.

Sammaye
  • 43,242
  • 7
  • 104
  • 146