問題描述
我有下圖中的結構,我想從id_Usuario"查詢的節點aaa1234"中獲取全部數據.我該怎么做這個查詢?
I have the structure in the image below and I want to get the whole data from the node "aaa1234" querying by "id_Usuario". How can I do that query?
我試過了:
DatabaseReference root = database.getReference().child("Eventos").child("participantes");
Query query = root.orderByChild("id_Usuario").equalTo(2);
為了更清楚一點,aaa1234"是自定義鍵,所以在下一個節點會有所不同.
Just to be more clear, the "aaa1234" is a custom key, so it will be different in the next node.
推薦答案
我看到了兩個錯誤.
第一個是錯字,我已經在評論中標明了.您將用戶 ID 存儲為數字,因此不應該在 equalTo 中的值周圍加上引號.所以:.equalTo(2)
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");
這將創建對不存在的子 Eventos/participantes
的引用.你想要做的是查詢 Eventos
,然后查詢屬性 participantes/id_Usuario
.所以
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);
這篇關于從 Firebase 實時數據庫 Android 查詢嵌套數據的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!