tags:

views:

25

answers:

1

I have a data set in MongoDB that involves a large set of emails and I need to be able to add emails to the set and being able to check if certain emails are in the set. I thought of doing with document structure like this:

{'key': 'foo', 'emails':['[email protected]','[email protected]', ...]}

and use $addToSet and $in. But the problem is that Mongo has 4MB document limit and if there's a lot of emails it could be not enough. I could split it info key/email parts but I'm worried it would make both matching (since emails aren't in one place now) and inserting (since I'd need to check for uniqueness) slower. So, what would be the best way to do this?

A: 

Depending on how many sets of emails you're going to have you could make each set of emails a collection where each document consisted of only the email address. You could then create a unique index on the email address:

db.foo.ensureIndex({email:1},{unique:true})

David O.
There is many sets, each with its own key (the search is withis emails with the same key). Index might work, but I'm worried it'd be slower since the emails won't be stored together.
StasM
If you're only caring about uniqueness inside one collection, creating a unique index is as fast as it's gonna get. That's exactly the purpose of using indexes.
David O.