2016-11-11 33 views
1

我想要SELECT * FROM products WHERE 'birthday' = true; 但是,我可以對Firebase數據庫執行相同的操作嗎?Android Firebase數據庫查詢選擇條件

我已經試過

DatabaseReference ref = FirebaseDatabase.getInstance().getReference(); 
ref. child("products").orderByChild("birthday").equalTo(true).addChildEventListener ........ 

Firebase Database image

但它並不work.Please幫助。如果我的數據結構不好,請告知如何創建更好的結構。

回答

2

數據庫的屏幕截圖顯示birthday不是產品節點的直接子節點:還有一個額外的步驟,即for_purpose

您的查詢必須遵循此模式,精確,因此更新您的查詢代碼:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference(); 
ref.child("products") 
    .orderByChild("for_purpose/birthday") 
    .equalTo(true) 
    .addChildEventListener ... 

請注意,這有效地工作,必須建立索引的每一個可能的for_purpose孩子在你的安全規則。或者至少對於你在查詢中使用的那些。索引必須與您的查詢完全匹配,僅在for_purpose上放置索引是不夠的。

對於這個問題,你至少需要如下:

{ 
    "rules": { 
    "products": { 
     ".indexOn": [ 
     "for_purpose/birthday", 
     "for_purpose/anniversary", 
     "for_gender/male", 
     ... 
     ] 
    } 
    } 
} 
+0

嗨vzsg,你能告訴我如何設置安全規則?它是否正確? { 「規則」:{ 「.read 」: 「AUTH = NULL」, 「 .WRITE 」: 「AUTH = NULL」, 「產品」:{ 「 .indexOn 」:「 for_purpose」 ,「for_gender」] } } } –

+0

嗨vzsg,你能告訴如何設置安全規則嗎?它是否正確? { 「規則」:{ 「.read 」: 「AUTH = NULL」, 「 .WRITE 」: 「AUTH = NULL」, 「產品」:{ 「 .indexOn 」:「 for_purpose」 ,「for_gender」] } } } –

+0

我使用示例規則編輯答案。 – vzsg