Wang Dan
Wang Dan

Reputation: 23

Firebase Query (Getting with child child property)

I am working on Android with Firebase.
I have problems on querying to get objects with child of child property.
I attached my data structure.

https://i.sstatic.net/SDcJh.png

I want to get groups that contains users with certain UserID.

Upvotes: 0

Views: 245

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

In this case you need to use denormalization which means that you need to create another node named userGroups in which you need to add all the groups of a specific user. You database should look like this:

Firebase-root
    |
    ---- userGroups
             |
             --- userId1
             |      |
             |      --- groups
             |            |
             |            --- groupId1: true
             |            |
             |            --- groupId2: true
             |
             --- userId2
                    |
                    --- groups
                          |
                          --- groupId3: true
                          |
                          --- groupId4: true

With this database structure you can get all the groups in which a user is apart. Just attach a listener on the desired user and get all the groups from the dataSnapshot object.

Upvotes: 1

Related Questions