Home > Net >  Firebase Database: get all child's data from childs
Firebase Database: get all child's data from childs

Time:09-06

I want to be able to differentiate my content between type and get all other data but I don't know how to get past the IDs after content.

I want every data from -NBC22CHEk3ebNxdlYOi, -NBC-Nfz6e5yppFlxU7S, -NBC1TVY-TzT7yfrSA3o and later be possible to get new video id's from existing and new users

/content/{user_uid}/{content_uid}

Database structure

If I want only the content from one user then I can write this:

val uid = FirebaseAuth.getInstance().uid
// url is just a placeholder for the real database url
val ref = FirebaseDatabase.getInstance(url).getReference("/content/$uid")

CodePudding user response:

I want all content with the type value "video" from all users.

That's actually not possible with your actual database structure. All queries in the Realtime Database work on a flat list of nodes, where the value on which you want to perform the filter/order on, must be at a fixed path under each direct child node.

Unfortunately, that's not your case, because you have a dynamic key under the content node, which is actually the UID, and which also means that you can't filter on type. If you want to allow that, then you have to create a flat list of videos, where the UID (SIH5...89j1) is a property inside each video object. This is already achieved since publisher_uid holds exactly that.

In short, remove the extra (UID) level from your database tree. Or, leave it as it is, and denormalize the data. Basically create a new structure like this:

/content/{content_uid}

See, there is no UID involved. If you're new to NoSQL databases, this might sound some kind of weird, but it's actually a quite common practice.

  • Related