0

I am attempting to query some hierarchical data in firebase. I'm having a little difficulty in figuring out how to query the following data structure:

{
  "orgs": {
    "-KBFXBBEyvgtfqMvU4pi": {
      "name": "ACME 123",
      "owner": "-K9IPqIUIuEFzLS0f_Pe",
      "users": {
        "-KBF_GhwTmXfR6Jce30t": {
          "email": "[email protected]",
          "permission": "editor",
          "userKey": "K99LV9cTjh1ovW1D5j2"
        },
        "-KBF_M533zzbUilGvAAW": {
          "email": "[email protected]",
          "permission": "editor"
        }
      }
    },
    "-KBFaKlJ8tfqjBQjAZgq": {
      "name": "ACME Alt LLC",
      "owner": "-K9IPqIUIuEFzLS0f_ZZ",
      "users": {
        "-KBFbD4trt9nyeHPUQbn": {
          "email": "[email protected]",
          "permission": "editor"
        }
      }
    }
  }
}

Specifically, I would like to find out if the email address "[email protected]" exists. But this is a little confusing for me since I need to search through 2 levels (orgs and users).

1 Answer 1

1

After reading some more documentation it seems I really shouldn't be nesting my data like this. I'll be honest, it seems kind of contrary to do this when working with a JSON schema that that is hierarchical. Anyway, this is what I'm looking to do now:

{
  "orgs": {
    "-KBFXBBEyvgtfqMvU4pi": {
      "name": "ACME 123",
      "owner": "-K9IPqIUIuEFzLS0f_Pe"
      }
    },
    "-KBFaKlJ8tfqjBQjAZgq": {
      "name": "ACME Alt LLC",
      "owner": "-K9IPqIUIuEFzLS0f_ZZ"
      }
    }
  },
  "orgMembership": {
    "-KBFXBBEyvgtfq7h381h": {
      "org": "-KBFXBBEyvgtfqMvU4pi",
      "email": "[email protected]",
      "permission": "editor"
  }
}

Then I can use the following query:

orgMRef.orderByChild("email").equalTo("[email protected]").once("child_added", function(snapshot) {
  console.log("found: " + snapshot.key());
});
Sign up to request clarification or add additional context in comments.

2 Comments

Yup. Alternatively, add another node memberOrgs where you keep the opposite of orgMemberships: the orgs that a user is a member of. With that in place, you can look up the info with a query: ref.child('memberOrgs').child(auth.uid).once('value' The only thing faster than a well-index query is doing no query at all, but accessing the data directly.
Thanks. I was planning on doing something similar to that in that I was going to add a property called org to my users data. The good news here is that my business requirements are that a user can only belong to one org. When the user signs up I try to find them in orgMembership and then assign their org if they match. My next challenge will be coming up with security rules for this data. orgMembership is essentially an invite/share mechanism.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.