0

I want to create a database structure for my realtime database in firebase. Right now I have list and those list have sublist. Those sublist have products and users.

To get the users in a sublist I defined his own object

"SubListUser" : {
 "SubListId1" : {
  "User1Id" : {
    "username" :"String",
    "photoUrl" : "String"
  },
  "User2Id" : {
    "username" :"String",
    "photoUrl" : "String"
  }
  "ListId": "ListId1"
}

And to get the products I have this JSON object

"SubListProducts" : {
  "SubListId1" : {
     "Products" : {
       "ProductId1" : {
         "name" : "String",
         "productPhotoUrl" : "String"
       }
     "ListId": "ListId1"
}

Those subLists are inside other lists. To know the sublist in a list I have this object

{ "Lists": 
    "List1": {
      "subList1" : "SubListId1",
      "subList2" : "SubListId2",
    }

}

If I want to know the products/users of a sublist is this okay? Because if I want to know the products/users I have to do 2 finds first find the list with their sublist, and then find the products/users of that sublist.

I don't know if this is good or should I redo my database?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
jorge.munin
  • 27
  • 1
  • 5
  • In most NoSQL databases there is no such thing as a proper data structure on its own. It all depends on the use-cases of your app, which we (and probably even you) don't know. As a general rule though, I prefer keeping nesting to a minimum - as also shown in the Firebase documentation on data structuring here: https://firebase.google.com/docs/database/web/structure-data#best_practices_for_data_structure – Frank van Puffelen Jun 01 '22 at 11:59
  • but it's okay to do multiple finds to get a data to avoid nesting list?. I know it all depends in the proyect, but in general which one is better? – jorge.munin Jun 01 '22 at 12:19
  • 2
    As said: there is no "in general" when it comes to NoSQL databases. If you're new to this, I recommend reading [NoSQL data modeling](https://highlyscalable.wordpress.com/2012/03/01/nosql-data-modeling-techniques/) and watching [Firebase for SQL developers](https://www.youtube.com/playlist?list=PLl-K7zZEsYLlP-k-RKFa7RyNPa9_wCH2s) – Frank van Puffelen Jun 01 '22 at 12:40

0 Answers0