Relm
Relm

Reputation: 8287

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.

Upvotes: 3

Views: 3627

Answers (3)

Aravin
Aravin

Reputation: 7097

Your path should be

User/UserId/Notifications/NotificationId/NotificationObject

Please find the suggestion from Firebase team from this link: https://cloud.google.com/firestore/docs/concepts/structure-data

Upvotes: 0

Gil Gilbert
Gil Gilbert

Reputation: 7870

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.

Upvotes: 2

Andy Strife
Andy Strife

Reputation: 729

That first Collection you have should be Users instead of App. Then that Users Collection contains each User Document, then a Document can have a Subcollection and Documents within it.

At root level, you can have multiple Collections.

You can try to stick with your actual Data Structure but maybe it is better to rearrange it a bit to fit Firestore Data Stucture.

Upvotes: 0

Related Questions