size document in MongoDB!!!

138 views
Skip to first unread message

bth...@gmail.com

unread,
Jul 27, 2012, 12:01:59 AM7/27/12
to mongod...@googlegroups.com
i use MongoDB 2.0.5
i have document with two format
First:
doc1={_id:{w1:"he thong",w2:"thong tin"},value:33}->Object.bsonsize(doc1)=65bytes
Second:
doc2={w1:"he thong",w2:"thong tin",value:33}->Object.bsonsize(doc2)=55bytes

When i insert doc1 and doc2 into sam collection
Then i run
Object.bsonsize(db.mycollection.find({"_id.w1":"he thong"})) => 48 bytes
Object.bsonsize(db.mycollection.find({"w1":"he thong"})) => 48 bytes
Why size of document before and after insert to collection is difference? and two documents with differ size (doc2<doc1) but they are same size after insert into collection?

Thanks alot!!!
Phuc

Nat

unread,
Jul 27, 2012, 1:24:31 AM7/27/12
to mongod...@googlegroups.com
Phuc,

When the data is saved into mongodb, the client driver will insert _id field if missing. That would explain what changes your document size.
Date: Thu, 26 Jul 2012 21:01:59 -0700 (PDT)
Subject: [mongodb-user] size document in MongoDB!!!
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

Hong Phuc Bui Thi

unread,
Jul 27, 2012, 2:17:21 AM7/27/12
to mongod...@googlegroups.com
Thanks!!
But it's not increase, it's decrease, i don't understand??? And each document has same size is 48 bytes? MongoDB pre-allocate size for document in MongoDB?

2012/7/27 Nat <nat....@gmail.com>

Sam Millman

unread,
Jul 27, 2012, 4:00:07 AM7/27/12
to mongod...@googlegroups.com
"First:
doc1={_id:{w1:"he thong",w2:"thong tin"},value:33}->Object.bsonsize(doc1)=65bytes
Second:
doc2={w1:"he thong",w2:"thong tin",value:33}->Object.
bsonsize(doc2)=55bytes"


I would say it is becuase of the padding factor used by the _id in the first document. Also the second documents size is taken regardless of _id insertion whereas the first document has _id embedded into it.

Also the more nested arrays you use the more padding will be taken into account within the size of an object.

Sam Millman

unread,
Jul 27, 2012, 4:05:03 AM7/27/12
to mongod...@googlegroups.com
"Then i run
Object.bsonsize(db.
mycollection.find({"_id.w1":"he thong"})) => 48 bytes
Object.bsonsize(db.mycollection.find({"w1":"he thong"})) => 48 bytes"

Calculating the size of a BSON document isn't the most reliable method to judge the size of it on disk. There are quite a few reasons why the documents differ to when you run it pre-storage and post-storage (i.e. allocation, extents, moving of documents, natural object compression via serialization etc etc). Client side will also likely deserialize objects within the document differing your sizes. There are numerous, almost infinite explanations.

Nat

unread,
Jul 27, 2012, 4:09:06 AM7/27/12
to mongod...@googlegroups.com
It returns 48 because you are trying to calculate bson size on the cursor instead of the object itself. Try using find_first() instead.
From: Hong Phuc Bui Thi <bth...@gmail.com>
Date: Fri, 27 Jul 2012 13:17:21 +0700
Subject: Re: [mongodb-user] size document in MongoDB!!!

Sam Millman

unread,
Jul 27, 2012, 4:46:31 AM7/27/12
to mongod...@googlegroups.com
Oh yea that would explain it actually...
Reply all
Reply to author
Forward
0 new messages