0

I am building a simple group chat application in Java, I have created the follwing structure in Firebase realtime database:

"GROUPS":{
  "-LuSuWF4JjYNyjJ2Y5c4" : {
    "GROUP_MEMBERS" : {
      "qyM8p4IgJ7cFOBDNWeFjlaMjNsk2" : {
        "dateAdded" : "23/11/2019",
        "permissions" : 0,
        "user" : "email@email.com"
      }
    },
    "dateCreated" : "23/11/2019",
    "description" : "A dummy test group",
    "name" : "Group 2"
  }

I need to get all of the Groups given a user's UID, I am trying to run the following query from java but it is not working.

    public static Query getGroupsByUser(String uid){
       return FirebaseDatabase.getInstance().getReference(GROUPS).orderByChild(GROUP_MEMBERS).orderByKey().equalTo(uid);
    }

how should I make this query? or Is there a better way to structure my database schema?

Please Help!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Your current structure makes it easy to find the members of a given group. It does not however make it easy to find the groups for a given user. For that you'll want to add an inverted index to your data structure, which is pretty much the inverse of what you have now: a list where you keep the groups for each user. For a longer explanation, see my answer here: https://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Nov 24 '19 at 16:08

1 Answers1

0

You can only use one order-by method at a time. Calling an order-by method multiple times in the same query throws an error.Read the documentaion either use orderByChild() or orderByKey()

https://firebase.google.com/docs/database/android/lists-of-data?authuser=0#sort_data

Naveen Kashyap
  • 372
  • 1
  • 9