Firebase数据库查询来自多个孩子 [英] Firebase database query from multiple childs

查看:270
本文介绍了Firebase数据库查询来自多个孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些关于Firebase数据库结构的问题。我几天前刚刚开始学习时,对此并不熟悉。基本上1个帐户可以有多个收据,1个收据可以有多个不同类型的项目,1个收据为1个商店,1个收据为1个货币。

我已经想出了数据库设计如下:

 收据{
accountID1:{
receiptID1:{
日期:
store:{
storeName:store1
storeAddr:addr1
}
货币:{
currencyName:currency1
currentcySymbol:$
}
totalAmount:50.00
}
receiptID2:{...}
}
accountID2:{...}
},
itemlists {
receiptID1:{
items:{
itemID1:{
type:food
name:snack
price:10.00
数量:2
}
itemID2:{...}
}
}
receiptID2:{...}
},
receiptIDsByStore {
storeID1:{
receiptID1:true
receiptID2:true
}
},
receiptIDsByCurrency {
currencyID1:{
receiptID1:true
receiptID2:true
}
},
store {
storeID1:{
storeName:store1
storeAddress:addr1
}
},
currency {
currencyID1:{
currencyName:currency1
currencySymbol:
}
}
itemIDsByType {
food:{
itemID1:true,
itemID2 :true,
}
entertainment:{
itemID3:true,
itemID4:true,
}
}


  • 有没有冗余的错误,我为上述设计?

  • 我可以从收据中得到每个用户的总支出,对吗?我可以像 accounts / accountID1 那样查询,以获得所有收据,然后总计总额。

  • >我怎样才能总结每个用户对每种类型的项目的总支出?例如,我想找食物。所以我查询 itemIDsByType / food ,然后得到 itemIDs 列表,然后从那里查询 item列表并检查 receiptID 是否属于该特定账户,那么它是获得单位价格?




    任何建议将不胜感激!

    编辑

     收据{
    accountID1:{
    receiptID1:{
    date:07/07/2017
    store:{
    storeName:store1
    storeAddr:addr1
    }
    货币:{
    currencyName:currency1
    currentcySymbol:$
    }
    totalAmount:50.00
    items:{
    itemID1:true,
    itemID2:true,
    }
    }
    receiptID2:{
    date:08/07 / 2017
    store:{
    storeName:store1
    storeAddr:addr1
    }
    货币:{
    currencyName:currency1
    currentcySymbol:$
    }
    totalAmount:20.00
    物品:{
    itemID3:true,
    itemID4:true,
    }
    }
    }

    accountID2:{
    receiptID3:{
    date:08/07/2017
    store:{
    storeName:store2
    storeAddr:addr2
    }
    货币:{
    currencyName:currency1
    currentcySymbol:$
    }
    totalAmount:100.00
    items:{
    itemID5:true,
    itemID6:true,
    }
    }
    }
    },
    items {
    itemID1:
    类型:食物
    名称:零食
    单位价格:10.00
    数量:2
    }
    itemID2:{
    类型:娱乐
    名称:游戏设备
    单价: 150.00
    数量:1
    }
    itemID3:{
    类型:食品
    名称:果汁
    单价: 4.00
    数量:1
    }
    itemID4:{
    类型:娱乐
    名称:新年衣服
    单价: 100.00
    数量:1
    }
    itemID5:{
    类型:医疗保健
    名称:发烧药
    单价:100.00
    数量:1
    }
    itemID6:{
    类型:healthcare
    名称:flu meds
    unitprice:100.00
    数量:1
    }
    },
    receiptIDsByStore {
    storeID1:{
    receiptID1:true,
    receiptID2:true,
    }
    storeID2:{
    receiptID3:true,
    }
    },
    receiptIDsByCurrency {
    currencyID1:{
    receiptID1:true,
    receiptID2:true,
    receiptID3:true,
    }
    $ b $ store $ {
    storeID1:{
    storeName:store1
    storeAddress:addr1
    }
    storeID2:{
    storeName:store2
    storeAddress:addr2
    }
    },
    货币{
    currencyID1:{
    currencyName:currency1
    currencySymbol:$
    }
    },
    itemIDsByType {
    food:{
    itemID1:true,
    itemID3:true,

    娱乐:{
    itemID2:true,
    itemID4:true,
    }
    保健:{
    itemID5:true,
    itemID6 :true,
    }
    }


    解决方案

    <你的数据库结构非常好。在Firebase数据库构建过程中,您需要记住的一件事就是让数据尽可能地扁平化,并为视图制作一切。如果你想了解更多关于Firebase数据库结构的知识,请阅读这篇文章: NOSQL数据建模技术构建您的Firebase数据正确的复杂的应用程序



    关于你的问题,你可能会看到在这些职位,有不同的位置相同的数据不是一个错误,实际上是相反的,这是Firebase中的普遍做法。是的,您可以查询像收据/ accountID1 并获取所有收据。如果您需要计数,您可以直接在 DataSnapshot 上直接使用 getChildrenCount()方法。 Firebase不禁止嵌套查询。您可以查询一次,获得这些ID,然后根据这些ID得到所需的数据。



    至少,如果您有SQL背景,我建议您注意这个YouTube的教程sesries, SQL开发人员的Firebase数据库



    希望它有帮助。


    I have some question regarding Firebase database structure. I am not familiar with it as I just started to learn it couple days ago. Basically 1 account can have many receipts, 1 receipt can have many items of different types, 1 receipt for 1 store and 1 receipt for 1 currency.

    I have came up with the database design as below:

    receipts {
        accountID1 : {
            receiptID1 : {
                date:
                store: {
                    storeName: store1
                    storeAddr: addr1
                }
                currency: {
                    currencyName: currency1
                    currentcySymbol: $
                }
                totalAmount: 50.00
            }
            receiptID2 : { ... }
        }
        accountID2 : { ... }
    },
    itemlists {
        receiptID1: {
            items: {
                itemID1 : {
                    type: food
                    name: snack
                    price: 10.00
                    quantity: 2
                }
                itemID2 : { ... } 
            }
        }
        receiptID2: { ... }
    },
    receiptIDsByStore {
        storeID1: {
            receiptID1: true
            receiptID2: true
        }
    },
    receiptIDsByCurrency {
        currencyID1: {
            receiptID1: true
            receiptID2: true
        }
    },
    stores {
        storeID1: {
            storeName: store1
            storeAddress: addr1
        }
    },
    currencies {
        currencyID1: {
            currencyName: currency1
            currencySymbol: $
        }
    }   
    itemIDsByType {
    food: {
        itemID1: true,
        itemID2: true,
    }
    entertainment: {
        itemID3: true,
        itemID4: true,
    }
    }
    

    So my question is:

    • Is there any redundancy mistake I made for the design above?

    • I can get the total amount of spend by each user from receipts, am I right? I can just query like receipts/accountID1 to get all the receipts then sum up the total amount.

    • How can I actually sum up the total spend by each user for each type of items? For instance, I wanted to find for food. So I query itemIDsByType/food, then get list of itemIDs, then from there query itemlists and check if receiptID is belonged to that particular account, it it is then get the unit price?

    Any suggestions will be appreciated! Thanks in advanced!

    EDIT

    receipts {
        accountID1 : {
            receiptID1 : {
                date : "07/07/2017"
                store : {
                    storeName : "store1"
                    storeAddr : "addr1"
                }
                currency : {
                    currencyName : "currency1"
                    currentcySymbol : "$"
                }
                totalAmount : "50.00"
                items : {
                    itemID1 : true,
                    itemID2 : true,
                }
            }
            receiptID2 : {
                date : "08/07/2017"
                    store : {
                        storeName : "store1"
                        storeAddr : "addr1"
                    }
                    currency : {
                        currencyName : "currency1"
                        currentcySymbol : "$"
                    }
                    totalAmount : "20.00"
                    items : {
                        itemID3 : true,
                        itemID4 : true,
                    }
            }
        }
    
        accountID2 : { 
            receiptID3 : {
                    date : "08/07/2017"
                        store : {
                            storeName : "store2"
                            storeAddr : "addr2"
                        }
                        currency : {
                            currencyName : "currency1"
                            currentcySymbol : "$"
                        }
                        totalAmount : "100.00"
                        items : {
                            itemID5 : true,
                            itemID6 : true,
                        }
                }
        }
    },
    items {
            itemID1 : {
                type : "food"
                name : "snack"
                unitprice : "10.00"
                quantity : "2"
            }
            itemID2 : { 
                type : "entertainment"
                name : "gaming equipment"
                unitprice : "150.00"
                quantity : "1"
            }
            itemID3 : { 
                type : "food"
                name : "fruit juice"
                unitprice : "4.00"
                quantity : "1"
            } 
            itemID4 : {
                type : "entertainment"
                name : "new year clothes"
                unitprice : "100.00"
                quantity : "1"
            }
            itemID5 : {
                type : "healthcare"
                name : "fever meds"
                unitprice : "100.00"
                quantity : "1"
            }
            itemID6 : {
                type : "healthcare"
                name : "flu meds"
                unitprice : "100.00"
                quantity : "1"
            }
    },
    receiptIDsByStore {
        storeID1 : {
            receiptID1 : true,
            receiptID2 : true,
        }
        storeID2 : {
            receiptID3 : true,
        }
    },
    receiptIDsByCurrency {
        currencyID1 : {
            receiptID1 : true,
            receiptID2 : true,
            receiptID3 : true,
        }
    },
    stores {
        storeID1 : {
            storeName : "store1"
            storeAddress : "addr1"
        }
        storeID2 : {
            storeName : "store2"
            storeAddress : "addr2"
        }
    },
    currencies {
        currencyID1 : {
            currencyName : "currency1"
            currencySymbol : "$"
        }
    },
    itemIDsByType {
        food : {
            itemID1 : true,
            itemID3 : true,
        }
        entertainment: {
            itemID2 : true,
            itemID4 : true,
        }
        healthcare : {
            itemID5 : true,
            itemID6 : true,
        }
    }
    

    解决方案

    Your database is very well structured. One thing you need to remeber when it comes to Firebase database structuring a is to have the data as flatten as possible and to make everything for the view. If you want to learn more about Firebase database structure i recomand you reading this posts: NOSQL DATA MODELING TECHNIQUES and Structuring your Firebase Data correctly for a Complex App.

    Regarding your questions, as you you'll probably see in those posts, having the same data in different location is not a mistake, actually it's the opposite, it's ca ommon practice in Firebase. Yes, you can just query like receipts/accountID1 and get all the receipts. If you need a count, you can just simply use getChildrenCount() method directly on the DataSnapshot. Nested queries are not prohibited in Firebase. You can query once, to get those ids and second to get the desired data according to those ids.

    Not at least, if you have a SQL background i recomand you watching this youtube tutorial sesries, The Firebase Database For SQL Developers.

    Hope it helps.

    这篇关于Firebase数据库查询来自多个孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆