Quick search for substrings in Mongo

We have a Mongo database with 400,000 entries, each of which has a relatively short (<20 characters) header. We want to be able to quickly find substrings in these headers (fast enough to be able to use results in things like auto-fill bars). We are also looking for prefixes (does the name begin with a substring). What we can do?

+4
source share
2 answers

If you perform only a preliminary search, then indexing this field should be sufficient. Root regular expression queries use an index and should be fast.

+5
source

Sergio is correct, but more specifically, the index on this and the left root prefix without the i flag (case insensitive) will use the index efficiently. This is actually noted in the docs:

http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-RegularExpressions

Do not forget to use . explain () if you want to check queries as well.

+2
source

All Articles