Reputation: 101
I want to retrieve the text 'Private Group' from the child 'group' IF the child 'contacts' has the value '9aIMkiMa0bSuMLjUk3R5bLpnoQS2'. I also want to check for the same value from the other child nodes of the parent child 'questions posts'. I have struggled with it but in vain. This is the code that I have been using, but It has only produced errors.
final DatabaseReference groupsRef = FirebaseDatabase.getInstance().getReference().child("questions posts");
Query query = groupsRef.orderByChild("group").orderByChild("contacts");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
arrayList.clear();
if (snapshot.exists() && snapshot.hasChild(onlineUserId)){
arrayList.add(groupsRef.getKey().toString());
arrayAdapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
This is the image of the database structure
Upvotes: 0
Views: 188
Reputation: 598728
A Firebase query operates on a flat list of nodes, and can only contain a single unknown key.
From what I can tell you have at least two levels of unknown keys (-MH...Ez12
, and 9alMk....QS2
) and possibly three (Private Group
). There is no way to query this structure with a database query, and you will need to come up with a different data structure to allow your use-case.
For example, if you want to allow finding the posts for a given contact ID, consider storing exactly that relationship:
"contactsToPosts": {
"$contactId": {
"$postId": true
}
}
For more on this sort data modeling trade-off, see:
Upvotes: 1