Using Mongodb to store a very large list of "blacklist" ids
I need to store an unordered list of IDs in mongo as a "blacklist", and use them purely for checking reasons.
blacklisted_ids= [1,23......100002942234... some very large number]
But storing it in a single document is not feasible because I will blow through the 2Mb l开发者_开发技巧imit very fast.
I can create a collection in mongodb, and insert each id as a document, but it seems like overkill, given that all I want is to check for existence.
What would be the right way to do this?
Note: I am using mongoid/rails. =)
Are these ids integers? Mongo's document size limit was raised to 16 MB in version 1.8, so you should be able to fit a few million integers in a single document with it.
If that's still not enough for your needs, I don't think a new collection would be overkill at all. Just be sure that you use the '_id' field to store the id you care about so that you don't waste any disk space:
class BlacklistedId
include Mongoid::Document
identity :type => Integer
end
Also, when you query it, make sure you do something like:
BlacklistedId.where(:_id => 78943).only(:_id).first
# A result means it's blacklisted, nil means it's not.
By telling Mongo that you only want the _id field returned, it'll serve your query straight out of the _id index and won't bother to seek out the actual document on the disk. This will keep things snappy, and will also mean that Mongo won't need to keep the actual collection in RAM - just the values in the index.
精彩评论