node.jsmongodbmonk

How do you perform a Case-Insensitive query in node.js implementations of mongodb?


I have a collection containing usernames. If a user searches for "joe", I want it to return "Joe", and any case-different variants (i.e. "jOe", etc.).

I'm using monk to implement mongodb queries in my node.js application - but the documentation has no mention of case insensitive queries, nor does it have any implementation of collations.

I am aware that regex can be used (and works), but it seems to go against the advice of the mongo documentation. Is there a regexless way of doing this in node.js implementations of MongoDB?

I'm unsure as to how I would implement a query like this - Any help would be appreciated.


Solution

  • MongoDB supports text search - as long as the field has the appropriate index you can leverage the $text operator which supports a caseSensitive option.