【问题标题】:Saving bitarrays in MongoDB在 MongoDB 中保存位数组
【发布时间】:2015-11-17 03:47:00
【问题描述】:

我正在我的公司构建 Bloom 过滤器,需要将它们序列化并保存到 MongoDB 中。我目前使用的文档结构如下:

{
    '_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
}

如您所见,位数组当前表示为字符串,而布隆过滤器位数组可以得到 huge 以获得更大的容量。我数据库中的单个文档现在大约 5 兆字节,这太糟糕了。

如果它有帮助,我正在用 Python 编程并使用 PyMongo 作为适配器。

我当然认为这不是保存位数组的正确方法,而且我在 Internet 上的其他地方找不到帮助。请帮帮我。

【问题讨论】:

    标签: python mongodb bitarray database


    【解决方案1】:

    我发现我可以使用 BSON 二进制数据类型来完成我的工作。 为了序列化我的布隆过滤器对象,我这样做了:

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

    请注意,bitarray 是 Python bitarray 库,而不是 BitString。最终的字典serialized 很容易插入到 MongoDB 中。

    希望这可以帮助任何寻找这个的人。 干杯:)

    【讨论】:

      猜你喜欢
      • 2011-09-16
      • 2018-05-27
      • 2021-07-12
      • 2017-01-13
      • 1970-01-01
      • 1970-01-01
      • 2015-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多