1

I have a structure where I need to login with an user below a certain parent and I need to know in which parent I'm in order to modify that parent and my user id, here is my database structure

enter image description here

Now, Im login to the app with one of the users id below users node, now, I need to know the parent for that user in order to modify that user below that parent

Example

I login with the user RgF82BBZXhahtgYeZDWXwCk1q6b2 , now I need to know that is inside 9MmrGOJz9ePksi.... parent in order to add data inside that parent and inside that user

Tried this code but it only do it for a random key and not for the key containing that user

mDatabase.child("MyApp").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                    String key = snapshot.getKey();

                    mDatabase.child("MyApp").child(key).child("users").child(mAuth.getCurrentUser().getUid()).child("apples").setValue("3");

                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
Todd
  • 179
  • 15

2 Answers2

2

With your current data structure there is no efficient way to look up the parent of a known user ID. The Firebase database doesn't support querying for the existence of a specific value in a list.

As is common with NoSQL databases, the solution is to add an additional data structure that does allow your user case. In this case that would be a simple inverse lookup, where the user ID is the key, and the parent key is the value.

Something like:

userLookup
  RgF82BBZXhahtgYeZDWXwCk1q6b2: 9MmrGOJz9ePksi.... 
  dRnalqAWm3............MM6vb2: 9MmrGOJz9ePksi.... 

Now you can easily look up the parent key for a given user ID, and then find their data in the existing structure.

For some more data modeling tips, see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks a lot Puf ! I just added an answer below on how I solved it :) – Todd Sep 28 '18 at 16:19
  • 1
    Good to hear that you got it solved Todd. And thanks for sharing. For future questions, please post the JSON as text and not as a screenshot, so that we can copy/paste it more easily. You can get this by clicking the "Export JSON" link in your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Sep 28 '18 at 17:11
1

If anyone interested I solved the issues like Puf suggested

First I created the lookupTable

enter image description here

And then a minor calls to get it done

 mDatabase.child("MyApp").child("userLookup").child(mAuth.getCurrentUser().getUid()).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                String key = dataSnapshot.getValue(String.class);
                mDatabase.child("MyActivity").child(key).child("users").child(mAuth.getCurrentUser().getUid()).child("apples").setValue(3);

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
Todd
  • 179
  • 15