Hello I am trying to query data in real time. my database structure looks like this:
{
"Businesses": {
"-MW_bnEkz0ZjRlgpaF5V": {
"name": "business 1",
"users": {
"8baHUbgQlbeiw9ydGHp6qZHm59I2": 0,
"Wcaa9RQTJIPh8jhFHs0S4laLcPD3": 1
}
},
"-MW_cYkt0hqhu0YSVQ_q": {
"name": "business 1",
"users": {
"Wcaa9RQTJIPh8jhFHs0S4laLcPD3": 0
}
}
},
"Users": {
"8baHUbgQlbeiw9ydGHp6qZHm59I2": {
"name": "user 1",
"businesses": {
"-MW_bnEkz0ZjRlgpaF5V": 0
}
},
"Wcaa9RQTJIPh8jhFHs0S4laLcPD3": {
"name": "user 1",
"businesses": {
"-MW_cYkt0hqhu0YSVQ_q": 0,
"-MW_bnEkz0ZjRlgpaF5V": 1
}
}
}
}
I want to get only the businesses which have specific user id under the users
child that equals to 0
I tried this:
private void listenToBusinessesUserJoinedTo(String userId){
Query query = FirebaseDatabase.getInstance().getReference("Businesses");
query.orderByChild("users/"+ userId).equalTo(User.JOINED); // User.JOINED = 0
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
businessesUserJoinedList.clear();
if (dataSnapshot.exists()){
for (DataSnapshot postSnapShot : dataSnapshot.getChildren())
businessesUserJoinedList.add(postSnapShot.getValue(Business.class));
}
updateUI();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
when I pass 8baHUbgQlbeiw9ydGHp6qZHm59I2
as the userId
I get back all the data under the Businesses
child and not only the business with the id
-MW_bnEkz0ZjRlgpaF5V
.
. what did I do wrong?