简体   繁体   中英

How to structure properly Firebase Database?

I read some Firebase database structure guides on how to structure your data properly (without data nesting) but I have one question. So, I have an iOS app that uses Firebase database. The users need to login/register. In terms of data structure, my database looks like this:

-Database
---Users
-----User1
--------username: johndoe
---------email: johndoe@test.com
---------display_name: John Doe
-----User2 {....}
-----User3 {....}

Now, let's imagine I have 100K users in there. Every time a new user is being registered, I check if the username & the email already exist in the database, if they don't then create the new user account.

My question is - Do I need to create a new object that contains only the usernames and another that contains only the emails? I'm asking this because I'm concerned that if I iterate through the Users objects I will potentially be downloading hundreds of megabytes just to check if the username and the email already exist.

Firebase will not allow duplicate users (authentication names). So when you call createUser, firebase will return an error if the user already exists.

Secondly, if you are performing a query for a specific item in Firebase, you are not downloading anything unless that item is found. So whether its 10 or 100k user nodes, nothing is downloaded when performing a query other than the nodes that match the query, which would only be one if there was a duplicate user. again though, this is not needed since Firebase rejects duplicate authentication names.

And to clarify; there is nothing wrong with nesting nodes. However, keeping them flat is usually better depending on your use case . So don't go overcomplicating your structure if you don't need to.

Oh, and your Firebase structure is spot on. Keep going with that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM