2017-10-04 107 views
0

我的火力數據庫看起來像這樣火力地堡規則來讀取父節點比葉/子節點

"students" : { 
    "firebase_key_1" : { 
    "Name" : "blah blah", 
    "Address" : "blah blah", 
    "Roll No" : "blah blah", 
    "Marks" : { 
     "Sub1" : "blah", 
     "Sub2" : "blah", 
     "Sub3" : "blah", 
     "Total" : "Total", 
    }, 
    "class" : "blah blah", 
    "Transportation" : "blah blah", 
    "Department" : "blah blah", 
}, 
    "firebase_key_2" : { 
    "Name" : "blah blah", 
    "Address" : "blah blah", 
    "Roll No" : "blah blah", 
    "Marks" : { 
     "Sub1" : "blah", 
     "Sub2" : "blah", 
     "Sub3" : "blah", 
     "Total" : "Total", 
    }, 
    "class" : "blah blah", 
    "Transportation" : "blah blah", 
    "Department" : "blah blah", 
}, 
    "firebase_key_3" : { 
    "Name" : "blah blah", 
    "Address" : "blah blah", 
    "Roll No" : "blah blah", 
    "Marks" : { 
     "Sub1" : "blah", 
     "Sub2" : "blah", 
     "Sub3" : "blah", 
     "Total" : "Total", 
    }, 
    "class" : "blah blah", 
    "Transportation" : "blah blah", 
    "Department" : "blah blah", 
} 
} 

我使用REST API來檢索火力點的數據。 RESTAPI URL看起來像https://domain.firebaseio.com/students.json?orderby=「標誌/總」 & startAt = 400

我已經通過火力規則索引學生總。我得到的結果以及額外的數據,如名稱,班級,卷號。

我所要的輸出是

"firebase_key_1" : { 
    "Marks" : { 
     "Sub1" : "blah", 
     "Sub2" : "blah", 
     "Sub3" : "blah", 
     "Total" : "Total", 
    } 
}, 
"firebase_key_2" : { 
    "Marks" : { 
     "Sub1" : "blah", 
     "Sub2" : "blah", 
     "Sub3" : "blah", 
     "Total" : "Total", 
    } 
}, 
"firebase_key_3" : { 
    "Marks" : { 
     "Sub1" : "blah", 
     "Sub2" : "blah", 
     "Sub3" : "blah", 
     "Total" : "Total", 
    } 
} 

反正通過RESTAPI或規則來做到這一點。

有沒有被讀取,我們可以定義哪些節點的任何規則,例如

{ 
"users":{ 
    "students":{ 
     ".read" : ["$firebaseKey/Marks"], 
     ".write" : true, 
    } 
    } 

,這樣我可以使用REST API來從父節點所需的值。

任何其他建議,這將是美好的。

在此先感謝

+0

我不明白你想在這裏完成什麼。你可以編輯你的問題來顯示:1)你想要做的REST調用,2)該位置的JSON(作爲文本),3)你想從調用返回的結果? –

+0

我已編輯上述問題。 – Prags

+0

這不是規則的有效語法:''.read「:[」$ firebaseKey/Marks「]'。 –

回答

1

Firebase數據庫始終返回完整的節點。無法獲得與您的查詢匹配的每個節點的子集。整個節點都返回,或者不返回。

通常,這種類型的請求表示您已合併應分離的多種類型的數據。就你而言,它看起來應該有兩個頂級集合:studentsstudentMarks。在students下,您保留每個學生的屬性,並以他們的學生ID爲鍵。在studentMarks下,您保留每個學生的標記,並再次輸入學生ID。

所以:

"students" : { 
    "firebase_key_1" : { 
    "Name" : "blah blah", 
    "Address" : "blah blah", 
    "Roll No" : "blah blah", 
    "class" : "blah blah", 
    "Transportation" : "blah blah", 
    "Department" : "blah blah", 
    }, 
    "firebase_key_2" : { 
    "Name" : "blah blah", 
    "Address" : "blah blah", 
    "Roll No" : "blah blah", 
    "class" : "blah blah", 
    "Transportation" : "blah blah", 
    "Department" : "blah blah", 
    }, 
    "firebase_key_3" : { 
    "Name" : "blah blah", 
    "Address" : "blah blah", 
    "Roll No" : "blah blah", 
    "class" : "blah blah", 
    "Transportation" : "blah blah", 
    "Department" : "blah blah", 
    } 
}, 
"studentMarks": 
    "firebase_key_1" : { 
    "Sub1" : "blah", 
    "Sub2" : "blah", 
    "Sub3" : "blah", 
    "Total" : "Total", 
    }, 
    "firebase_key_2" : { 
    "Sub1" : "blah", 
    "Sub2" : "blah", 
    "Sub3" : "blah", 
    "Total" : "Total", 
    }, 
    "firebase_key_3" : { 
    "Sub1" : "blah", 
    "Sub2" : "blah", 
    "Sub3" : "blah", 
    "Total" : "Total", 
    } 
} 

由於您使用students之間的相同的密鑰studentMarks您可以輕鬆地準備兩套用於用戶數據。但是現在,您也可以只讀取每個用戶的屬性,或者只讀取一組用戶的標記。

+0

非常感謝,我會改變結構。 – Prags

+0

我認爲會有這樣的語法「.read」:[「$ firebaseKey/Marks」]。類似於indexOn,這就是爲什麼我認爲我可以堅持這種結構。無論如何非常感謝。 – Prags