0

This is how my Database looks like.

The table group has child "currentMembers" The table group has child "currentMembers"

In "currentMember", the first string is userID, and the next one is a boolean. I want to select group which contain a specific userID. My code is showing below.

Query query = FirebaseDatabase.getInstance()
            .getReference()
            .child("groups").orderByChild("currentMembers").equalTo(currentUserID);

    FirebaseRecyclerOptions<UserGroup> options =
            new FirebaseRecyclerOptions.Builder<UserGroup>()
                    .setQuery(query, UserGroup.class)
                    .build();

But it return nothing, how could I fix it?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Slim
  • 13
  • 4

1 Answers1

0

You're looking for:

FirebaseDatabase
  .getInstance()
  .getReference()          
  .child("groups")
  .orderByChild("currentMembers/"+currentUserID)
  .equalTo(true);

But this won't scale very well, since you'll need to explicitly define an index for each member.

This typically means that you need to adapt your data model to include a map that allows the inverse lookup. So a node that allows looking up the list of groups for a particular user.

users
  $uid
    currentGroups
      $groupId: true

Also note that you're nesting multiple entity types in a single branch of your tree, which is an anti-pattern when it comes to the Firebase Database.

If you have two entity types Users and Groups and a many-to-many relationship between them, you'll end up with four top-level lists:

users
groups
userGroups
groupUsers

Also see my answers on these related questions:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807