简体   繁体   English

从DB检索的文档的BSON对象大小

[英]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.

相关问题 pymongo-数组的mongo文档bson大小 - pymongo - mongo document bson size of an array 通过pymongo从MongoDB中获取bson文档并将其转换为json后,访问json对象字段时出错 - Error accessing json object fields after bson document fetched from MongoDB via pymongo and converted to json MongoDB $ nin查询超出了BSON文档大小限制 - MongoDB $nin query exceeds BSON document size limitation 使用pymongo查询mongo DB时删除BSON对象ID - Remove BSON Object ID when querying mongo DB using pymongo Django,Python,尝试更改从数据库对象检索到的对象中的字段值/属性。所有调用均不起作用 - Django, Python, trying to change field values / attributes in object retrieved from DB objects.all call, not working 错误:document必须是dict,bson.son.SON,bson.raw_bson.RawBSONDocument的实例,或者是从collections.MutableMapping继承的类型。 - error :document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping 从Django中检索的模型对象访问属性 - Access attributes from retrieved model object in django BSON无法编码对象 - BSON can not encode object Python:从从数据库检索的字段中删除引号 - Python: remove quotes from field retrieved from DB Django视图:将从db检索的unicode转换为字符串 - Django view: Convert unicode retrieved from db to string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM