Inacius Inacius - 5 months ago 20
Android Question

Query nested data from Firebase Real-time database Android

I have the sctructure in the imagem below and I want to get the whole data from the node "aaa1234" querying by "id_Usuario". How can I do that query?

This is my structure

I tried:

DatabaseReference root = database.getReference().child("Eventos").child("participantes");
Query query = root.orderByChild("id_Usuario").equalTo(2);

Just to be more clear, the "aaa1234" is a custom key, so it will be different in the next node.

Answer Source

I see two mistakes.

The first one is a typo, which I already marked in my comment. You're storing the user ID as a number, so shouldn't have quotes around the value in equalTo. So: .equalTo(2)

The second mistake is in the way you try to query:

DatabaseReference root = database.getReference().child("Eventos").child("participantes");

This will create a reference to the non-existing child Eventos/participantes. What you want to do instead is query Eventos, but then against property participantes/id_Usuario. So

DatabaseReference root = database.getReference().child("Eventos");
Query query = root.orderByChild("participantes/id_Usuario").equalTo(2);