Question

In my collection, my documents seem to be relatively similar. I need to know how much storage I'll need in my production server to store my documents, that's why I queried :

db.collectionName.stats()

My field avgObjSize was around 6442 bytes. I chose a random document in my collection, I typed :

Object.bsonsize(db.collectionName.find({"_id" :         ObjectId("5508497c51a990da07b07106")}))

And I obtained 810 bytes.

Why do I have this huge difference ?

I also noticed that more I store documents, more avgObjSize decreases... Is that normal ? (I checked, my documents are not empty.)

It's a development server, so I'm on Red Hat Enterprise Linux 5.8 and it's a 32bit version.

Was it helpful?

Solution

My field avgObjSize was around 6442 bytes. I chose a random document in my collection, I typed :

Object.bsonsize(db.collectionName.find({"_id" : ObjectId("5508497c51a990da07b07106")}))

And I obtained 810 bytes.

Why do I have this huge difference ?

The db.collection.find() method returns a cursor, so what you have calculated is the size of the cursor object rather than the size of a document.

You should use db.collection.findOne() to return a single document if you want to measure the BSON size:

Object.bsonsize(db.collectionName.findOne(
    {"_id" : ObjectId("5508497c51a990da07b07106") }
))

I also noticed that more I store documents, more avgObjSize decreases... Is that normal ? (I checked, my documents are not empty.)

The average is based on the size of the documents in the collection, so it sounds like your trend over time is to insert smaller documents. The normal behaviour depends entirely on your use case ;-)

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top