Nested Documents

When to use nesting of data types and when not to ??

Hi @Shivam_86502,

Ideally we use nesting in our data model when there is a natural hierarchy in the data that we are dealing with.

One very simple case would be how I store the address of a specific person in my database. Here is how I would generally store it in my database.

{
"Name": "Andrew Jones",
...
...

"address": {
             "House_no": 123,
             "Street_name": "Bank street",
             "City": "Brooklyn",
             "Country": "United States",
             "Area_code": 917
           }
}

As you can see here, we have used a nested document to store the address of a person.

This is a data modelling related question. If you want to know more about it then you can check out M320: Data Modeling course in which we talk about the best practices of data modelling and schema design in MongoDB.

If you still have any questions, please feel free to get back to us.

Happy Learning :slight_smile:

Thanks,
Shubham Ranjan
Curriculum Support Engineer

1 Like

Just wanted to ask / clarify if my understanding is correct from M320 that as a general rule of thumb we should look to embed rather than reference to take advantage of MongoDB rich document structure.

I know there are still cases that are better suited for references.
Thanks @Shubham_Ranjan

@Shubham_Ranjan Thanks a lot . It helped and Yes, I’ll refer to the course " M320: Data Modeling" that you’ve suggested above.