[英]BSON object size of document retrieved from DB
Mongo shell have the bsonsize() method to get BSON size of a given document retrieved from DB. Mongo shell使用bsonsize()方法来获取从DB检索的给定文档的BSON大小。
Is there any way of getting the same using PyMongo driver? 有没有办法使用PyMongo驱动程序获得相同的? I have found the bson module in the documentation but it is not fully clear to me how to use it to get the size of a document retrieved from DB.
我在文档中找到了bson模块 ,但我不清楚如何使用它来获取从DB检索的文档的大小。
Based on @SSDMS suggestion (thanks!) the following can be used: 根据@SSDMS建议(谢谢!),可以使用以下内容:
len(bson.BSON.encode(document))
I have tested with a couple of documents in my DB, comparing the result at mongo shell and with the above Python method and getting the same result: 我已经在我的数据库中测试了几个文档,比较了mongo shell和上面的Python方法的结果,得到了相同的结果:
At mongo shell: 在mongo shell:
> var doc1 = db.entities.findOne({'_id.id': '001'})
> var doc2 = db.entities.findOne({'_id.id': '002'})
> Object.bsonsize(doc1)
816
> Object.bsonsize(doc2)
819
At Python console: 在Python控制台:
>>> import bson
>>> from pymongo import MongoClient
>>> db = MongoClient('localhost', 27017)
>>> doc1 = db['orion']['entities'].find_one({'_id.id': '001'})
>>> doc2 = db['orion']['entities'].find_one({'_id.id': '002'})
>>> len(bson.BSON.encode(doc1))
816
>>> len(bson.BSON.encode(doc2))
819
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.