0

I have a database like below, I want to query the accounts based on user ID.

  "accounts": [
    {
      "applications": [
        {
          "appId": "sfdsfsfsfsfsfsfsfweufisfsfsfshwfkwjkf",
          "creationDate": "dadasffsfsfsfsfsfsfsfsfsf",
          "name": "Application one",
          "users": [
            {
              "userId": "0Mo0J6Y7aUXdhwUszYrP5hy3k1C2"
            },
            {
              "userId": "0Mo0J6Y7aUXdhwUwszYrP5hy3k1C2qqqqqq"
            },
            {
              "userId": "0Mo0J6Y7aUXdhwUszYrP5hy3kqqqqqqq1C2qqqqqq"
            }
          ]
        },
        {
          "appId": "sfdsfsfsfsfsfsfsfweufisfsfsfshwfkwjkf",
          "creationDate": "dadasffsfsfsfsfsfsfsfsfsf",
          "name": "Application two",
          "users": [
            {
              "userId": "0Mo0J6Y7aUXdhwUszYrP5hy3k1C2"
            },
            {
              "userId": "0Mo0J6Y7aUXdhwUwszYrP5hy3k1C2qqqqqq"
            },
            {
              "userId": "0Mo0J6Y7aUXdhwUszYrP5hy3kqqqqqqq1C2qqqqqq"
            }
          ]
        }
      ],
      "id": "ahdagjhjfsfjgsfguwuwiuwireij",
      "name": "Account ONE"
    },
    {
      "applications": [
        {
          "appId": "sfdssfsfs242fsfsfsfsfsfsfweufihwfkwjkf",
          "creationDate": "dadasffsfsfsfsfsfsfsfsfsf",
          "name": "application one",
          "users": [
            {
              "userId": "dadassfsfs24242424ffsfsfsfsfsfsfsfsfsf"
            },
            {
              "userId": "0Mo0J6Y7aUXdhwUszYrP5hyaaaaaaaa3k1C2qqqqqq"
            },
            {
              "userId": "0Mo0J6Y7aUXdhwUszYrP5haaaay3kqqqqqqq1C2qqqqqq"
            }
          ]
        }
      ],
      "id": "ahdagjhjfsfjgsfguwuwiuwireij",
      "name": "Account TWO"
    }
  ]
}

I am new to Firebase. How can I retrieve a result from the data above where userID = '0Mo0J6Y7aUXdhwUszYrP5hy3k1C2'?

How can I get the accounts/application object?

I tried using OrderByChild/OrderBy but did not work. like

      .orderBy('userId')
      .equalTo('0Mo0J6Y7aUXdhwUszYrP5hy3k1C2')
      .on('child_added', function(snapshot) {
        var movie = snapshot.val();
        console.log(JSON.stringify(movie));
      });
Sandeep
  • 55
  • 7
  • 1
    Did you try anything yet? If not, I recommend starting with the [documentation](https://firebase.google.com/docs/database) or the codelab for your platform. – Frank van Puffelen Jul 28 '21 at 14:53
  • Your data structure makes it easy to find the users for a given account/application. It does not however make it easy to find the accounts/applications for a given user. To allow that, you'll need to add an additional data structure where you map each UID back to the accounts/applications they have. For more on this, see my answer here: https://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value and https://stackoverflow.com/questions/41527058/many-to-many-relationship-in-firebase – Frank van Puffelen Jul 28 '21 at 15:00

0 Answers0