Correct practice for querying groups in Firebase

Viewed 71

I'm trying to understand groups in Firebase database. I've created a simple index called "genres" which I've placed inside my stations node:

"STATIONS": {
    "GENRES": {
        "D&B": {
            "RODR": false
        },
        "HipHop": {
            "RODR": false
        },
        "TRANCE": {
            "RODR": true
        }
    },
    "RODR": {
        "PROMOTED": false,
        "downvote": 2,
        "image": "image url",
        "station": "Revolution Of Dance Radio",
        "tag": "We Are The Revolution",
        "upvote": 1001,
        "url": "to be added"
    },
    "UKNRADIO": {
        "Promoted": false,
        "downvote": 2,
        "image": "url for image",
        "station": "UKN RADIO",
        "tag": "The Home Of The Mashup",
        "upvote": 1001,
        "url": "to add"
    }
},

Then I have these children in a separate node with the key set to the same as the index key.

How do I query the genre node to get all items in the trance index? Which in this example would be RODR?

Edit

This value is to gain access to the right child with in my Genres node:

String value = getArguments().getString("Genre");
    Log.i("Genre_bundle",value);

This is the basic code for showing my list:

String DATABASE_CHILD = "STATIONS";
    ref = db.getReference().child(DATABASE_CHILD);

Edit 2

I've tried setting up the query in a separate listener to get the key I need; this works, but only if there's a single item in the node. As soon as I add a second, for example uknradio= true it won't show.

 String DATABASE_CHILD = "STATIONS";
    ref = db.getReference().child(DATABASE_CHILD).child("GENRES").child(value);

    Query query = ref.orderByValue().equalTo(true);

    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot dataSnapshot: snapshot.getChildren())
                KEYS = dataSnapshot.getKey();

                Log.i("KEY", KEYS);
                getStations(KEYS);
                

        }

 private void getStations(String Key) {
    String DATABASE_CHILD = "STATIONS";
    ref1 = db.getReference().child(DATABASE_CHILD);
    ref1.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot snap : snapshot.getChildren()) {
                String key_Checker = snap.getKey();

                if (KEYS.matches(key_Checker)) {
                    Log.i("KEY", "Present");
1 Answers

If you want to read the stations for the TRANCE genre, that's a two-step process:

  1. Read the /STATIONS/GENRES/TRANCE path to get the keys of the stations.
  2. Loop over the keys and read each station's info in turn.

In code with hardcoded values that'd loop like:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference genreRef = rootRef.child("STATIONS/GENRES/TRANCE");
genreRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot genreSnapshot) {
        for (DataSnapshot stationKeySnapshot: genreSnapshot.getChildren()) {
            String stationKey = stationKeySnapshot.getKey();

            DatabaseReference stationRef = rootRef.child("STATIONS").child(stationKey);
            stationRef.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot stationSnapshot) {
                    Log.i("DB", stationSnapshot.child("station").getValue(String.class);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    throw databaseError.toException();
                }
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException();
    }
}
Related