3

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.

Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
Relm
  • 7,923
  • 18
  • 66
  • 113
  • Hi Try to flatten the firebase structure , create separate node for notifications , under user id store user's notifications...if you use nested one it will take time to fetch data – Shanmugam Oct 09 '17 at 06:27
  • @Shanmugam Please ignore time, I still need to this exactly as I asked. – Relm Oct 09 '17 at 06:32
  • what issue you are getting , what is your requirement – Shanmugam Oct 09 '17 at 06:35
  • My requirement is to get json in the format I specified on the question. – Relm Oct 09 '17 at 06:36
  • while adding to firebase give database ref like this users/uid/notifications and create a map with name "notif-object" and add data, push to above reference with auto gen id – Shanmugam Oct 09 '17 at 06:42
  • 1
    Collections and Documents are different things and you can't treat them interchangeably. Mapping raw JSON to Cloud Firestore without adjusting it to match Cloud Firestore's data model is probably not going to work well. You'll want to either stick to Realtime Database (which is more flexible in its data model), or tweak your data model to work better with Cloud Firestore. – Michael Lehenbauer Oct 09 '17 at 16:04

3 Answers3

2

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.

Gil Gilbert
  • 7,722
  • 3
  • 24
  • 25
  • 1+, "but the structure of collections and documents is not itself JSON.", ok, I wasn't getting that part. I'll have to look into this further. – Relm Oct 11 '17 at 15:00
0

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.

Andy Strife
  • 729
  • 1
  • 8
  • 27
  • Why "should" it be "Users", I don't think that's correct. Any mature sort of database shouldn't enforce that kind of thinking. – Relm Oct 10 '17 at 06:27
  • I said"should", not "must". There is a difference there. My point was that you can have multiple root collection. Firestore is only Firebase that allow deep nesting WHEN necessary but is still a no SQL database – Andy Strife Oct 10 '17 at 18:16
  • @Relm I don't think this answer deserves a downvote. What Andy meant is that you need to start with a collection as that's how Firestore structure works. It's not just a JSON object store. – gligoran Dec 24 '17 at 23:01
0

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

Aravin
  • 6,605
  • 5
  • 42
  • 58