0

I have had several hundreds of players play a game I created, and each team's score is located down this path on my firebase structure:

myref.firebaseio.com/UK/date here/Tournaments/time here/player name here/Final Score

The last key is "Final Score", followed by the team's score as the value.

The problem is that the date here, time here, and player name here nodes are different depending on the day and time the game was played and the name the player chose.

I want to simply extract each score from all the players and add them in an array, in order to calculate the average.

I am looking for a way to tell the query "find me a "Final Score" key, several sub nodes down, 3 of them with unknown names, and get me the value. Do this for all entries in the database"

I have managed to go 1 level down with an unknown path , but no more than 1 level.

Reading about deep queries did not help me. Is it possible to do this, or I will have to change my database structure to achieve this?

Thanks in advance for any help

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Apneist
  • 396
  • 7
  • 17

2 Answers2

2

On Firebase Realtime Database a query can contain only one unknown level. In other words, a query operations on a list of nodes, not on a tree of nodes. If you have multiple nested level, you will need to find a different way to search it. That can either be client-side (for small sets of data), or by storing a flat list with the information you want to query.

In your case the latter would be a flat list of "final score" values, regardless of their date and time.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • That was my suspicion also Frank - i will be going for the flat list solution going forward. Many thanks for the clarification – Apneist Sep 22 '19 at 18:04
1

You need to do loop over all unknown values to get finalscore .. something like this

 ref.child("UK").observeSingleEvent(of: .value) { (snap) in
            let dates  = snap.children.allObjects as! [DataSnapshot]
            for date in dates{ // To get all Unknown dates
                let tournmentSnap = date.childSnapshot(forPath: "Tournaments").children.allObjects as! [DataSnapshot]
                for unkownTime in tournmentSnap{  // To get all Unknown Time
                    let playerSnap = unkownTime.children.allObjects as! [DataSnapshot]
                    for play in playerSnap{ // To get all Unknown Players
                        print(play.value) // Final Score
                    }
                }
Gurtej Singh
  • 225
  • 1
  • 9