問題描述
我正在使用 Firebase 創建一個 NoSQL 數據庫,但由于我是一名 SQL 思想家,因此我需要一些關于 Firebase 查詢和 NoSQL 數據結構的建議.
I’m creating a NoSQL database using Firebase, but as I’m a SQL thinker my mind needs some advices about Firebase queries and NoSQL Data Structure.
我正在使用 Ionic 2 (AngularJS 2) 為移動應用程序做一些實驗,我擔心的是速度(用戶時間)和我的表格的正確結構.
I am making some experiments for a Mobile App using also Ionic 2 (AngularJS 2), and my concerns are about the speed (user time) and the correct structure of my tables.
在應用程序中,我列出了用戶所做的事情,并且用戶可能是組的一部分,現在在 Firebase 網站的非規范化之后,我創建了這個架構
In the App I have a list of things that Users have Made and Users could be part of a Group, now following the de-normalization from the Firebase website I made this schema
Users : {
Id : {
Name: string
Picture: base64 56x56 pixels (image)
Addresses : {}
Groups : {
Worker: boolean
Student: boolean
}
}
}
Groups : {
Worker : {
Desc: string
Members : {
Userid: boolean
}
}
Student : {
Desc: string
Members : {
Userid: boolean
}
}
}
Made : {
Id : {
Name: string
Thumb: base64 56x56 pixels (image)
Desc: string
User: userid
Images: base64 dimensions could vary
}
}
我要做的是顯示用戶所做的事情的列表,其中包含很少的信息,例如名稱和拇指圖像,單擊它會打開帶有其他信息的描述.
What I have to do is to show a list of things that an user made, with few information like name and thumb image, by clicking on it opens the description with the other infos.
現在,如果我使用以下代碼段獲取列表,它會返回所有信息,但一開始我只需要姓名和拇指(或其他內容)
Now if I use the following snippet for getting the list, it returns me all the info but at the beginning I need just name and thumb (or something else)
return firebase.database().ref('/made/' + madeId)
.once('value').then(function(snapshot) {
var username = snapshot.val().username;
// ...
});
那么,firebase 中有什么方法可以做類似(?)的事情:
So, is there any way in firebase to do something like(?):
SELECT name, thumb FROM Table_Made
為了只獲取每個查詢我需要的字段?或者
In order to get only the fields that I need for each query? Or
最好有另一個只有名稱和圖像的表嗎?
Is better to have another table with only the name and the image?
同時擁有拇指和圖像是個好主意嗎?為了加速json,因為拇指一般比圖片小.
Is a good idea to have both thumb and the image? In order to speed up the json because the thumb is generally smaller than the image.
查看 Firebase 文檔,似乎小于 10mb 的圖像可以以 base64 格式保存到文檔中,以防存儲空間更大.您對此有何看法?
Looking at Firebase doc, it seems that images smaller than 10mb could be saved as base64 into the document and in case are bigger on the storage. What is your idea about that?
推薦答案
Firebase 客戶端總是檢索完整的節點.無法僅獲取節點屬性的子集.
Firebase clients always retrieve complete nodes. There is no way to get just a subset of the properties of a node.
像往常一樣:如果您在數據模型上實現用例時遇到問題,請更改數據模型以適應您的用例.所以在這種情況下,這意味著您可以將名稱和縮略圖拆分為一個單獨的節點.
As usual: if you are having problems implementing your use-case on the data model, change the data model to fit your use-case. So in this case, that means that you could split off the name and thumbnail into a separate node.
MadeListinfo : {
Id : {
Name: string
Thumb: base64 56x56 pixels (image)
}
}
這篇關于NoSQL 數據結構,用于使用 Firebase 和查詢選擇字段子集的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!