0

I have a structure like so:

"games" : {
    "03e46939-af4e-4b9a-ba3b-99132f0bf34d" : {
      "1P0mWbp1jYOayaMEyQcnKvX4YB63" : {
        "logs" : {
          "-LDxpYtVZJc4LmJmyzn1" : {
            "guess" : "1234",
            "result" : "1P 1B",
            "score" : 150
          }
        },
        "maxbulls" : 1,
        "maxpigs" : 1,
        "moves" : 1,
        "profilepicture" : "bull.png",
        "score" : 150,
        "username" : "user1p0mw"
      },
      "5AvSPiyH3faqw1P7inHdyjV6noC2" : {
        "maxbulls" : 0,
        "maxpigs" : 0,
        "moves" : 0,
        "profilepicture" : "pig.png",
        "score" : 0,
        "username" : "user5avsp"
      },
      "answer" : "8103e4693964",
      "id" : "03e46939-af4e-4b9a-ba3b-99132f0bf34d",
      "requestor" : "1P0mWbp1jYOayaMEyQcnKvX4YB63",
      "status" : "pending",
      "turn" : ""
    },
    "21b1451-rf45-4b9a-ba3b-99132fgoe856" : {
      "5AvSPiyH3faqw1P7inHdyjV6noC2" : {
        "logs" : {
          "-LSwzYtVZJp9s6gmuw7n" : {
            "guess" : "1234",
            "result" : "1P 1B",
            "score" : 150
          }
        },
        "maxbulls" : 1,
        "maxpigs" : 1,
        "moves" : 1,
        "profilepicture" : "pig.png",
        "score" : 150,
        "username" : "user5avsp"
      },
      "answer" : "8103e4693964",
      "id" : "21b1451-rf45-4b9a-ba3b-99132fgoe856",
      "requestor" : "5AvSPiyH3faqw1P7inHdyjV6noC2",
      "status" : "pending",
      "turn" : ""
    }
  }

Where there are many games with unique GUIDs and containing keys of 1-2 UIDs. I want to get all games where 5AvSPiyH3faqw1P7inHdyjV6noC2 is a child of a game. I do not know the game ID(s) at the time of the query.

I have tried this.afDatabase.list('/games/', ref => ref.orderByChild(user.uid)) which results in ALL games being pulled in. EDIT: No matter what UID is passed in. I realized this example was bad because in this case I would want all games returned.

And this.afDatabase.list('/games/', ref => ref.orderByChild(user.uid).equalTo(user.uid)) which pulls in no games.

And this.afDatabase.list('/games/', ref => ref.orderByChild().equalTo(user.uid)) which throws an error about orderByChild not having any arguments passed in.

Tristan
  • 1,608
  • 1
  • 20
  • 34
  • 1
    Your current structure allows you to efficiently retrieve the child nodes (what look like UIDs) in a given game. It doesn't not allow you to efficiently retrieve the games for a given UID. To allow that, consider adding an inverse structure `/users/$uid/$gameid`. Also see https://stackoverflow.com/questions/27207059/firebase-query-double-nested – Frank van Puffelen Jun 02 '18 at 01:09
  • @FrankvanPuffelen Sorry for the very long reply time. I have a page that lists all of the games for a given UID. The page also displays some attributes of the game like the status and opponent profile picture. If I maintain a list of GameIDs on each user that is in the game, will I need to duplicate all of that used game info? Or is there an easy way to take a list of GameIDs and get each game object? – Tristan Aug 29 '18 at 18:17
  • Yes. Duplication is quite common in NoSQL databases. If you come from a background of relational databases that might take some getting used to. I recommend reading [NoSQL data modeling](https://highlyscalable.wordpress.com/2012/03/01/nosql-data-modeling-techniques/) and watching [Firebase for SQL developers](https://www.youtube.com/playlist?list=PLl-K7zZEsYLlP-k-RKFa7RyNPa9_wCH2s). – Frank van Puffelen Aug 29 '18 at 22:40

0 Answers0