Reputation: 1120
Okay, so I have a collection full of folks' emails. I want to efficiently look up the domain of the email without altering the existing data.
Currently I can look up the username SUPER fast since it's a regex scan that is prefix based, my collection is about 1GB+ in size and my server isn't super powerful. I do have an index on "Email". The fast query is something like:
db.emails.find({"Email": {'$regex':'^johnsmith'}})
My index is simple and looks like this:
db.emails.createIndex({ Email: 1 })
However, if I try to find the domain, I have to use a non-prefixed regex scan like this, but this results in a query that still uses the index, but takes about 10-20x more time:
db.emails.find({"Email": {'$regex':'sampledomain.com'}})
I've tried using a suffix instead of a prefix like this, but still just as slow:
db.emails.find({"Email": {'$regex':'sampledomain.com&'}})
I'm not sure if there's some sort of index I can make just on the domain portion of the email, but I'm pretty new to mongoDB so any advice would be appreciated.
Upvotes: 1
Views: 567
Reputation: 14520
If you are searching from the beginning of domain, you can extract the domain and store it in another field, at which point you'd be able to use prefix regexp match on that field.
Upvotes: 1