I encountered a strange behavior of mongo and I would like to clarify it a bit...
My request is simple as that: I would like to get a size of single document in collection. I found two possible solutions:
Here, I provide some code I perform testing on:
I created new database 'test' and input simple document with only one attribute: type:"auto"
db.test.insert({type:"auto"})
output from stats() function call: db.test.stats():
{ "ns" : "test.test", "count" : 1, "size" : 40, "avgObjSize" : 40, "storageSize" : 4096, "numExtents" : 1, "nindexes" : 1, "lastExtentSize" : 4096, "paddingFactor" : 1, "systemFlags" : 1, "userFlags" : 0, "totalIndexSize" : 8176, "indexSizes" : { "_id_" : 8176 }, "ok" : 1
}
output from bsonsize function call: Object.bsonsize(db.test.find({test:"auto"}))
481
With its 16Mb limit, a MongoDB document can easily store around 2 million values of 64-bit numbers (also dates and booleans). But strings are a special case. Each UTF-8 character takes one byte.
The maximum size an individual document can be in MongoDB is 16MB with a nested depth of 100 levels. Edit: There is no max size for an individual MongoDB database.
Find() Method. In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents.
GridFS is the MongoDB specification for storing and retrieving large files such as images, audio files, video files, etc. It is kind of a file system to store files but its data is stored within MongoDB collections. GridFS has the capability to store files even greater than its document size limit of 16MB.
In the previous call of Object.bsonsize()
, Mongodb returned the size of the cursor, rather than the document.
Correct way is to use this command:
Object.bsonsize(db.test.findOne())
With findOne()
, you can define your query for a specific document:
Object.bsonsize(db.test.findOne({type:"auto"}))
This will return the correct size (in bytes) of the particular document.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With