Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Firestore : How to structure data

Well, according to docs, this answer https://stackoverflow.com/a/46639620/2873357, collections and documents should altenate.

I need my data to be structured this way :

"app" :{
     "users" :{
              "$uid" :{
                      "notifications":{
                                     "auto-gen-id" :{
                                                   "notif-object":{
                                                                 "type":"",
                                                                 "subType" : ""
                                                    }
                                      }
                      }
               } 
      }
}

So, as I understand this, this is,

      : "app (collections)/
              users (document)/
                    $uid (collections)/
                         notifications (document)/
                              auto-gen-id (collections)/ 
                                   "notif-object" (document)/
                                                  type (field),
                                                  subType (field) 

I can't seem to achieve this sort of a thing in the Firebase console.

like image 852
Relm Avatar asked Nov 16 '25 06:11

Relm


1 Answers

If I understand you correctly, the thing you're trying to do isn't possible. The requirement that collections and documents alternate means that you can't really directly nest collections within collections. Faking it by using your $uid as a collection won't end up working well at all.

So the customary solution when there's no actual document to put in a collection is to name a fixed document that you don't intend to ever create. For example if there's only ever the one app for now, insert an extra "0", the app name, or something like that in the path:

app/0/users/userId/notifications/{auto-gen-id}

Within the auto-gen-id notification document you'd then have your type and subtype fields.

Note that unlike the Firebase RTBD Firestore is not a single giant JSON document. Each document within a collection is JSON, but the structure of collections and documents is not itself JSON.

like image 175
Gil Gilbert Avatar answered Nov 18 '25 20:11

Gil Gilbert