JayKay 135
JayKay 135

Reputation: 33

Fetch specific child data for multiple parents

I use the Firebase real-time database to store the stats of several games for my Discord bot and would like to calculate the high score for each game independently.

{
  "users" : {
    "123456" : {
      "name" : "userA",
      "tictactoe" : {
        "won": 5,
        "lost": 3
      },
      "4wins" : {
        "won": 2,
        "lost": 1
      },
      "hangman" : {
        "correct": 24,
        "incorrect": 12
      }
    },
    "234567" : {
      "name" : "userB",
      "tictactoe" : {
        "won": 7,
        "lost": 2
      },
      "4wins" : {
        "won": 6,
        "lost": 3
      },
    }
}

So, for example, if I need the hangman data, I only want to get the hangman data from each user (if it exists) with just one API call. I could of course load the full user data, but that would probably be quite inefficient.

Is it possible to use some kind of wildcard for the userid to only get the data for one game, or would I need to restructure the way I store data?

Upvotes: 0

Views: 34

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600126

The Firebase Realtime Database always returns complete nodes. There is no way to get it to return only a subset of the properties of each node.

If you find you have a common need to only get the data for one particular game for each user, consider adding an additional data structure where you keep only the information about that game for each user. This sort of data duplication is fairly common in NoSQL databases.

I also recommend reading the Firebase documentation on data structuring, specifically the sections on building nests and flattening data structures.

Upvotes: 1

Related Questions