Flat vs Nested structure


I am creating a model for a user entity. I work with a small team of developers and we are exchanging a lot of discussions while using the flat vs nested approach.

The premise is that the User Schema has a bunch of fields such as name, email ID, phone number, address, etc, which I would prefer mostly to be a flat model ( Except for maybe fields like address ).

We have a few more fields, Like 10 to 15 fields, and my senior is advising that we nest the fields according to some category: Such as profile_information OR contact_information or other groupings.

I would like you guys to help me in trying to understand what the drawbacks are to either of the approach. I personally don’t prefer a nested model here because I don’t see a need to group fields. My senior mentions that the reason for grouping is so that it’s easier to access the data and improves readiibility which I don’t agree with.

Also, is there any impact on the performance to nesting fields? I don’t foresee the document to grow too large.

Thanks for the help!

1 Like

I like nested objects. Mainly because you may $project an object in a single statement rather than projecting individual fields one by one. The benefit of a single $project is that when you add fields in the object you do not have to modify your pipelines to get the new fields. You schema is more flexible. For example, it is easier to go from 1 address object to multiple address objects. It is also easier to write and test code that only modify an object.

As for readability, I think the following speak by itself.

profile :
    name : "steevej" ,
    first : "Steeve" ,
    last : "Juneau"
contact :
  name : "lukes" ,
  first : "Luke" ,
  last : "Skywalker"


profile_name : "steevej" ,
proflle_first : "Steeve" ,
profile_last : "Juneau" ,
contact_name : "lukes" ,
contact_first : "Luke" ,
contact_last : "Skywalker"

But I am a senior too. So I might be biased by my past experience just like your seniors.

1 Like

Thanks @steevej. I have to agree that the data looks better structured but I didn’t think about code modifying only a part of the data!