[SERVER-7152] estimating document size for storage Created: 25/Sep/12  Updated: 11/Jul/16  Resolved: 15/Jan/13

Status: Closed
Project: Core Server
Component/s: Storage
Affects Version/s: 2.0.3
Fix Version/s: None

Type: Question Priority: Minor - P4
Reporter: Shanthi Nellaiappan Assignee: Scott Hernandez (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

We are trying to estimate the storage size for a given schema ..

On creating just a single document in a collection with only one attribute of type long results in avgObjSize of 36 bytes. What is the break up for this 36?
PRIMARY> db.test64Long.stats()
{
"ns" : "quad.test64Long",
"count" : 1,
"size" : 36,
"avgObjSize" : 36,
"storageSize" : 4096,
"numExtents" : 1,
"nindexes" : 1,
"lastExtentSize" : 4096,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 8176,
"indexSizes" :

{ "_id_" : 8176 }

,
}
PRIMARY> db.test64Long.findOne();

{ "_id" : ObjectId("5061db7f77eee6137922f6cf"), "key" : NumberLong(12345) }

On creating a document with int, the avgObjSize is 32 bytes.
PRIMARY> db.testInt.stats();
{
"ns" : "quad.testInt",
"count" : 1,
"size" : 32,
"avgObjSize" : 32,
"storageSize" : 4096,
"numExtents" : 1,
"nindexes" : 1,
"lastExtentSize" : 4096,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 8176,
"indexSizes" :

{ "_id_" : 8176 }

,
"ok" : 1
}
PRIMARY> db.testInt.findOne();

{ "_id" : ObjectId("5061df0777eee6137922f6d4"), "key" : 1234 }

On creating a document with an array of just one element of type long, the avgObjSize 44. What is the break up for 44 bytes.
PRIMARY> db.testAr2.findOne();

{ "_id" : ObjectId("5061dde977eee6137922f6d3"), "key" : [ 123456 ] }

PRIMARY> db.testAr2.stats();
{
"ns" : "quad.testAr2",
"count" : 1,
"size" : 44,
"avgObjSize" : 44,
"storageSize" : 4096,
"numExtents" : 1,
"nindexes" : 1,
"lastExtentSize" : 4096,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 8176,
"indexSizes" :

{ "_id_" : 8176 }

,
"ok" : 1
}



 Comments   
Comment by Scott Hernandez (Inactive) [ 25/Sep/12 ]

As Tad said, you can see the definition for the storage format of documents here: http://bsonspec.org/#/specification

You can also use Object.bsonsize(<doc>) to get the size of any document/object.

> Object.bsonsize({a:1, b:2})
27
> Object.bsonsize({})
5
> Object.bsonsize({a:ObjectId(), b:2})
31
> Object.bsonsize({a:ObjectId(), b:NumberLong(2)})
31

To estimate the indexes and actual storage size you will probably want to create a bunch of data and extrapolate based on the numbers.

Comment by Tad Marshall [ 25/Sep/12 ]

You can use mongodump to export this document to a BSON file and examine it in a hex editor. See http://bsonspec.org for the BSON specification.

Generated at Thu Feb 08 03:13:46 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.