1

Hi and thanks for helping. I need to insert data to a MongoDB collection and I confused, which approached is better, nested or flat way.

For example: one nested document

{ 
   a: [ {
     aName: 'name',
     b: [ {
       bName: 'name'
     } ]
}

or many flat and small documents

{
  a: 'name',
  b: 'name'
}

Comments:

  • there isn't update query, just insert (and get)

  • the nested object is not huge (not overflow the limit)

my thinks for the nested way:

  • it's more verbose, its help to the next person that looking on the collection to understand the hierarchy

  • the get is simpler, and the code for get is more readable

my thinks for the flat way:

  • maybe in the future should do more aggregeation on the collection, it will be easier.
shkediy
  • 21
  • 5
  • The approach really depends on your application's use case. As this is too subjective I suggest you come back with more concrete problem. – Mike Tung Mar 07 '19 at 16:29
  • I know my case as I wrote, in general, but I think there is a best practice and what not to do. – shkediy Mar 07 '19 at 17:22
  • 1
    There are always exceptions, best practice is a guideline not a mandate – Mike Tung Mar 07 '19 at 19:22
  • The question really is to general and "best practice" is really about *"how you need to use the data"*. You can see [Updating a Nested Array with MongoDB](https://stackoverflow.com/q/23577123/2313887) for more information of difficulties and *general* structural advice. Also [Find in Double Nested Array MongoDB](https://stackoverflow.com/q/29071748/2313887) demonstrates difficulties in the query side. Most importantly there are *"shades"* here as you probably don't really mean "flat", but you also don't want "deeply nested". – Neil Lunn Mar 08 '19 at 00:12

0 Answers0