eliamyro
eliamyro

Reputation: 308

Firebase query in tree with depth and multiple children

I have a problem regarding querying the firebase database using a value and getting the specific node. My schema is shown here:

Firebase schema

In my schema 'workLocations' belongs to an 'excavationWorks', and 'excavationWorks' belongs to an 'excavationLists'.

That means that the path to a specific workLocation is excavationLists/excavationWorks/workLocations/(specific workLocation)

The problem that I have is that I want to query the workLocations node by the location value (let's say London) and get the parent node (the red circled key which is the key of the specific workLocation).

I have search and read many posts but I haven't managed to make it work.

My code looks like this:

 DatabaseReference reference = FirebaseDatabase.getInstance().getReference();

 Query query = reference.child("workLocations").orderByChild("location").equalTo("London");
 query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot workLoc : dataSnapshot.getChildren()) {
                    // do something with the individual "issues"
                    Log.d(TAG, workLoc.getKey());
                }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Thank you

Upvotes: 2

Views: 1439

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

To achieve this, you need to query your database twice like this:

DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
DatabaseReference workLocationsRef = reference
    .child("excavationLists")
    .child("excavationWorks")
    .child("workLocations");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
            for(DataSnapshot ds : dSnapshot.getChildren()) {
                String key = ds.getKey();

                Query query = workLocationsRef.child(key).orderByChild("location").equalTo("London");
                ValueEventListener eventListener = new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot snapshot) {
                        String description = snapshot.child("description").getValue(String.class);
                        Log.d("description", description);
                        String partentKey = snapshot.getRef().getParent().getKey();
                        Log.d("partentKey", partentKey);
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {}
                };
                query.addListenerForSingleValueEvent(eventListener);
            }               
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
workLocationsRef.addListenerForSingleValueEvent(valueEventListener);

Upvotes: 1

Related Questions