generaltsow
generaltsow

Reputation: 205

Fast Substring Search in Mongo

We have a Mongo database with about 400,000 entries, each of which have a relatively short (< 20 characters) title. We want to be able to do fast substring searches on these titles (fast enough to be able to use the results in things like autocomplete bars). We are also only searching for prefixes (does the title start with substring). What can we do?

Upvotes: 4

Views: 1611

Answers (2)

Adam Comerford
Adam Comerford

Reputation: 21692

Sergio is correct, but to be more specific, an index on that and a left-rooted prefix without the i (case-insensitivity) flag will make efficient use of the index. This is noted in the docs in fact:

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

Don't forget to use .explain() if you want to benchmark the queries too.

Upvotes: 2

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230461

If you only do prefix searches then indexing that field should be enough. Rooted regex queries use index and should be fast.

Upvotes: 5

Related Questions