Saving bitarrays in MongoDB
Question: I'm building Bloom filters at my company and need to serialize and save them
into a MongoDB. The document structure I'm currently using looks like this:

    
    
    {
        '_id': unique ID,
        'm': number of bits in Bloom filter,
        'n': capacity of Bloom filter,
        'k': number of hashes,
        'bitarray': a string of the Bloom filter’s bitarray,
        'seeds': a list of seeds for the k hashes
    }
    

As you can see, the bitarray is currently represented as a string and Bloom
filter bitarrays can get _huge_ for a larger capacity. A single document in my
database is around **5 megabytes** right now which is terrible.

If it helps in anyway, I am programming in Python and using PyMongo as an
adapter.

I certainly believe this is not the right way to save bitarrays and I can't
find help elsewhere on the internet. Help me out please.


Answer: I figured out I could use the BSON binary data type to get my work done. To
serialize my Bloom filter objects, I did this:

    
    
    from bson.binary import Binary
    
    obj = BloomFilter()
    serialized = obj.__dict__.copy()
    serialized['bitarray'] = Binary(obj.bitarray.tobytes())
    

Please note that bitarray is the Python
[bitarray](https://pypi.python.org/pypi/bitarray/) library and not
`BitString`. The final dictionary `serialized` was easy to insert into a
MongoDB.

Hope this helps out anyone looking for this. Cheers :)

