MongoDB – Do Subdocuments Count Towards Total Document Size?

mongodbmongoose

I'm wondering if you had something like a list and a subdocument of items. Does MongoDB save them all as a single document, or does MongoDB regard the subdocuments as independent documents. This is in regard to the 16mb document limit.

E.g.

list document:

{
  items: [
    {
      _id: 1
      name: "item1"
    }
    {
      _id: 2
      name: "item2"
    }
    {
      _id: 3
      name: "item3"
    }
  ]
}

Best Answer

MongoDb manages json documents in a binary bson format. In particular, bson is used to store documents in the database and exchange the documents with the outside world.

Document with embedded/nested documents will be stored in a bson object that contains all its elements, including the nested ones. There is no automatic disassembling and reassembling. In fact, all the access to the nested documents will go through the owning document.

The 16 MB limit is meant to facilitate the management of documents in memory and in remote function calls. It therefore applies to the whole bson document including all its nested elements. There's by the way a limit of 100 to the nested depth.

More information:

Related Topic