简体   繁体   English

pymongo-数组的mongo文档bson大小

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

I'm trying to capture the array size(bson size in bytes) using pymongo. 我正在尝试使用pymongo捕获数组大小(bson大小以字节为单位)。 Below is the sample document structure, 下面是示例文档结构,

在此处输入图片说明

I'm able to retrieve the entire document size using following code snippet, 我可以使用以下代码段检索整个文档的大小,

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

But I'm unable to calculate the size of each sub array, example code which is failing, 但是我无法计算每个子数组的大小,失败的示例代码,

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))))

Output with Error: 输出错误:

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"]}']]

Expected output: 预期产量:

doc_size: 363953
custom_prop_size: *****

You should be able to limit what you get back from MongoDB by using projection like this: 您应该能够通过使用如下投影来限制从MongoDB获得的收益:

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

Then you can simply get the size of the entire document. 然后,您可以简单地获取整个文档的大小。

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

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