繁体   English   中英

pymongo-数组的mongo文档bson大小

[英]pymongo - mongo document bson size of an array

我正在尝试使用pymongo捕获数组大小(bson大小以字节为单位)。 下面是示例文档结构,

在此处输入图片说明

我可以使用以下代码段检索整个文档的大小,

for doc in src_db.src_coll.find({'customProperties':{ '$exists' : True }}).limit(2):
    print(len(bson.BSON.encode(doc)))

但是我无法计算每个子数组的大小,失败的示例代码,

for doc in src_db.Quotes.find({'customProperties':{ '$exists' : True }}).limit(1):
    print('doc_size:'+str(len(bson.BSON.encode(doc))))
    doc_customProp =  doc["customProperties"]
    print('custom_prop_size:'+str(len(bson.BSON.encode(doc_customProp))))

输出错误:

doc_size: 363953
Traceback (most recent call last):
  File "./document_stats.py", line 15, in <module>
    print('custom_prop_size:'+str(len(bson.BSON.encode(doc_customProp))))
  File "/usr/lib64/python2.7/site-packages/bson/__init__.py", line 976, in encode
    return cls(_dict_to_bson(document, check_keys, codec_options))
TypeError: encoder expected a mapping type but got: [[u'custValue', u'{"identifier":"","productId":["3"]}']]

预期产量:

doc_size: 363953
custom_prop_size: *****

您应该能够通过使用如下投影来限制从MongoDB获得的收益:

src_db.Quotes.find({'customProperties':{ '$exists' : True }}, {"_id": 0, "customProperties": 1})

然后,您可以简单地获取整个文档的大小。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM