2014년 12월 13일 토요일

how to call Object.bsonsize(db.test.findOne()) using db.command in pymongo

I am trying to check the document size in mongodb. I am using the python driver.

I am serializing a matrix into binary, I want to know the document size. For example:

#...
nrows
= 300,
ncols 
= 270
week 
= 14
month 
= 07
year 
= 2010
#...

band 
= np.empty((nrows,ncols))
# Filling the array with an value
band
.fill(3)
# Serializing band
serialized_band 
= Binary(pickle.dumps(band, protocol=2), subtype=128 )
          
# inserting serialized_band into a dictionary
db
.collection.insert({'week':week,'month':month, 'year':year,band:serialized_band})

I have tried to use:

result = db.command("Object.bsonsize","db.collection.find_one()")

or 

result = db.eval("Object.bsonsize(db.collection.find_one())")


on both, I receive the following error:

OperationFailure: command SON([('Object.bsonsize', 'db.collection.find_one()')]) failed: no such cmd: Object.bsonsize
Any ideas?



Also I have tried with:

from bson import SON
db.command(SON([('eval', 'Object.bsonsize'), ('args', "db.chlorophyll_2.find_one()")]))

without success.

The closest that I can figure it out is to use the sys module and just:

sys.getsizeof(doc)

Any other ideas, how to work properly with the db.command ? 



You don't need the server for this:

import bson
len(bson.BSON.encode({'week':week,'month':month, 'year':year, band:serialized_band}))



Thanks that works!


댓글 없음:

댓글 쓰기