Gastón Saillén
Gastón Saillén

Reputation: 13179

OrderByChild adding one more element to the result

I'm trying to get just the events that a certain user has, the thing is that after doing orderByChild() it's filtering one more event that I don't want.

The code

//I find the user that belongs to a certain Tutor
        mDatabase.child("tutorjugadores").child(mAuth.getCurrentUser().getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshotJugador) {
                for(final DataSnapshot snapshotJugadoresTutor : dataSnapshotJugador.getChildren()){
                    if(dataSnapshotJugador.exists()){

                        //If the user has not assisted to a certain event, I get the user that belongs to the user and get the event ID
                        mDatabase.child("asistenciasjugadores").orderByChild(snapshotJugadoresTutor.getKey()).addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshotEvento) {
                                for(DataSnapshot snapshotEventos : dataSnapshotEvento.getChildren()){
                                    //Getting the event key from that user

                                    String eventKey = snapshotEventos.getKey();
                                    Log.d(TAG, "Event : "+eventKey + "  of the user "+snapshotJugadoresTutor.getKey());
                                }


                            }

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

                            }
                        });

                    }
                }
            }

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

            }
        });

Now, this is my database structure

enter image description here

The problem

Now, when I run this code and execute the log to get the event for the user 2UHHZWR6B8P9XJPuLYrAg6aEgN63, I'm getting one more event that does not belong to that user, that event is this one -LU930PpCaQXT8CvUbc8

My log

Event : -LU930PpCaQXT8CvUbc8 of the user 2UHHZWR6B8P9XJPuLYrAg6aEgN63

Event : -LTRz61EcBUx-RLSSEaU of the user 2UHHZWR6B8P9XJPuLYrAg6aEgN63

Event : -LTSGFGdW1nQbtd9HOBh of the user 2UHHZWR6B8P9XJPuLYrAg6aEgN63

Event : -LTSIG_lqckgHEZ1q9YS of the user 2UHHZWR6B8P9XJPuLYrAg6aEgN63

As you can see, is listing all the events for that user, but that user only has 3 events associated with it.

When I order with this line

.orderByChild(snapshotJugadoresTutor.getKey())

I'm expecting to get just the nodes with the userID 2UHHZWR6B8P9XJPuLYrAg6aEgN63 and then loop and get each event ID from that user, but instead, I'm getting the event ID from this user as well GVD0bLDf41VDdraN67mXjAS6KPk1.

Upvotes: 0

Views: 30

Answers (1)

DeveloperKurt
DeveloperKurt

Reputation: 788

You can't filter with a string by only using JSON based databases like Firebase. And orderByChild only changes the sorting of the returned values. So you are basically ordering all the values under that key, there is no filtering going on. After this point you have 3 options to go on.

Option 1 - Not efficient, recommended for only small apps: You can order all the values and do the filter afterwards.

Option 2 - Recommended: You can change your data structure. Create another node like "UserEvents". Under it store UID's. And under that UID's there should only be that user's events.

Option 3 - Recommended and functional: You can use Elasticsearch. It's great for complex queries. You can use it with Firebase with Google Cloud Functions after you switched to " Pay as you go " plan or you can integrate another server with Firebase database by using Flashlight extension.

Upvotes: 1

Related Questions