0

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.

zkldi
  • 94
  • 1
  • 7
  • Looks like a dupe of https://stackoverflow.com/questions/1863399/mongodb-is-it-possible-to-make-a-case-insensitive-query. Lots of options in the answers there. – JohnnyHK Jan 11 '20 at 02:00
  • Also https://stackoverflow.com/questions/22021576/like-search-into-mongodb-with-monk-library may help you – L. Faros Jan 11 '20 at 02:01
  • HI, yes - I've seen these posts and they don't work formally with **node.js** implementations of mongoDB (namely monk - but also mongoose), as they don't have implementations for collations. The remaining option is regex, which is against mongoDB's advice. – zkldi Jan 11 '20 at 02:04

1 Answers1

2

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

James
  • 80,725
  • 18
  • 167
  • 237