0

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?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
No Name
  • 1
  • 1
  • Why aren't you looking the business like that up under `/Users/$uid`? The same information exists there (yay! good job denormalizing this data) as far as I can see, and you don't need a query. – Frank van Puffelen Mar 25 '21 at 15:23
  • Thanks for the answer! I want to to create a list of all the businesses the user joined to and show it in a ListView. Should I loop under `/Users/$uid/businesses` and add a listener for every business id with 0 as the value? – No Name Mar 26 '21 at 11:26
  • Yes, it seems to me that is the whole purpose for creating the reverse index under `Users`. It might help to re-read my explanations on this structure under https://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value and https://stackoverflow.com/questions/41527058/many-to-many-relationship-in-firebase – Frank van Puffelen Mar 26 '21 at 13:42

0 Answers0