来自多个孩子的Firebase数据库查询

时间:2017-07-07 03:51:36

标签: firebase firebase-realtime-database

我对Firebase数据库结构有一些疑问。我不熟悉它,因为我几天前就开始学习它。基本上1个账户可以有很多收据,1个收据可以有很多不同类型的商品,1个商店有1个收据,1个货币有1个收据。

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

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,
}
}

所以我的问题是:

  • 我上面的设计是否存在冗余错误?

  • 我可以从收据中获得每个用户的总支出,对吗?我可以像receipts/accountID1一样查询以获取所有收据,然后总结总金额。

  • 如何实际总结每个用户对每种商品的总支出?例如,我想寻找食物。所以我查询itemIDsByType/food,然后获取itemIDs的列表,然后从那里查询itemlists并检查receiptID是否属于该特定帐户,然后获取单位价格?

任何建议将不胜感激!先谢谢!

修改

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,
    }
}

1 个答案:

答案 0 :(得分:3)

您的数据库结构良好。在涉及Firebase数据库结构化时,您需要记住的一件事是让数据尽可能扁平化,并为视图创建所有内容。如果您想了解有关Firebase数据库结构的更多信息,请重新阅读以下帖子:NOSQL DATA MODELING TECHNIQUESStructuring your Firebase Data correctly for a Complex App

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

至少,如果你有一个SQL背景,我会重新审视这个youtube教程sesries,The Firebase Database For SQL Developers

希望它有所帮助。

相关问题