Search code examples
mongodbwebweb-applicationsnosqlbson

Can somebody please explain the size limit of a document in MongoDB? It says there is a 16MB limit per document but what does it mean?


I have about 3 collections and each don’t even have a lot of data. Each of them have about 50 objects/items that each contain around 200 characters but the whole collection (one collection) is taking up ≈270KB of space (which is a lot). I don’t understand why it is doing that.

So going back to the question, do those collections each have a limit of 16mb or is it the entire database? Please help. Thank you.


Solution

  • Here are some examples:

    Object.bsonsize({ a: null }) => 8
    Object.bsonsize({}) => 5
    Object.bsonsize({ _id: ObjectId() }) => 22
    
    Object.bsonsize({a: "fo"}) => 15
    Object.bsonsize({a: "foo"}) => 16
    Object.bsonsize({ab: "fo"}) => 16
    

    5 Bytes seems to be smallest possible size.

    You can retrieve the BSON size of your documents with this aggregation pipeline:

    db.collection.aggregate([{ $project: { size: { $bsonSize: "$$ROOT" } } }])
    

    The max size of a document is 16 MiByte, which is a hard-code limit. Each document has the _id field with typically an ObjectId value, thus the minimum size of a document is 22 Byte.

    According to MongoDB: The Definitive Guide the entire text of "War and Peace" is just 3.14MB, so 16MiB is quite a lot.