1-1 relation performance, querying flat V.s nested doc!

At first glance, querying a flat doc (without a nested doc) looks better for performance. It was surprising to see the tutor used nested doc merely for to keep things clear:


Is not there any difference in performance when querying nested v.s flat doc?

1 Like


This is a good question.
With the current implementation of the storage engine in MongoDB, the documents get entirely loaded in memory. Then the BSON structure is parsed in order to extract the different fields. This operation of parsing the document is done in memory and is really fast compared to the other operations like finding documents and index keys in the trees, which are also fast, however, these will require more computation cycles.
In other words, having an embedded structure or not is not going to make a difference for performance.

I recommend grouping information in sub-documents because it will make it easier to manage, especially on the application side where this grouping may match the objects in the application.


Here are some reasons why I prefer sub-document.

  1. If you only what to retrieve and process the address you can project the address sub-document and get all the fields. In a flat document you have to project all the fields of interest which might change.
$project : { address : 1 }
$project : { street : 1 , city : 1 , zip 1 }
  1. It is much easier to move from a sub-document to an array of sub-document than it is to move from a flat document with name mangling.

  2. It simplifies code. For example, if you print address label, your code take an address sub-document and simply print the fields, street, city, zip … With name mangling you have to somehow indicate with a parameter that you what to print the shipping_* fields rather than non-mangled names.