How to Structure Your Data | Get to know Cloud Firestore #5
Вставка
- Опубліковано 26 лис 2024
- It's important to get your Cloud Firestore data structure in order. Knowing when to use Maps, Arrays and Subcollections can get a little tricky. In the last episode of Get to Know Cloud Firestore, we focused mainly on theory. But now, we’re going to put that into practice and show you how you can structure your Cloud Firestore data the right way for your app. Let us know what you think in the comments below, and stay tuned for the next episode which focuses on security rules!
Choose a data structure doc → bit.ly/2NMAkDW
Watch Pt.1 of Maps, Arrays and Subcollections → bit.ly/2RZ7bsr
More episodes on Cloud Firestore → goo.gle/Cloud-...
Subscribe to Firebase → goo.gle/Firebase
Hi Todd from 2020. You might want to add that we can now search for multiple attributes in an array using the 'array contains any' query! Great videos!
YES! This is exactly what I need! Thank you for sharing @Arvind Shekar. @Firebase Todd 2022, please update and with lists of pros & cons. Thanks :) if you even read these comments... I notice no replies.
@@pagevpetty Page.. haven't I seen you in the Academind comments section? :D!
@@AmeliorScout Probably... from a while ago.
Love this kind of tutorial. No code, just explaining concepts of how to structure data. Looking forward to the security rules video.
With visuals :) Visual examples (note squares) work!
Love how you're keeping these videos current. Well done 2019 Todd
But where’s 2021 Todd? Is he ok and are there no updates?
This is one of the best technical talks I've seen. Super clean, super simple to follow, and really explains the trade offs. Thanks for making Firebase best practices/structure easy to understand!
It would also be nice to see bullet lists of pros and cons for each.
This series is the best. These last two videos in particular have been especially helpful, addressing questions I've been thinking about for months. I look forward to the next installment.
Trying to make sense of the firebase universe, this has been a massively informative series so far in understanding how it all works! So Thank you. PLEASE update the series with understanding permissions soon!
big fan of the teaching style, going over things more than once
Well done Todd 2018 and 2019, love your vids. Can't wait for Todd 2020! :)
The example at 13:23, when we get the Favorite Restaurants, would it be a bad idea to use composite ids of the User and Restaurant documents for my FavoriteRestaurant documents? Assuming I have more Users than Restaurants, I would use something like ${user-id}_${restaurant-id} for my FavoriteRestaurant id. Assuming my Users and Restaurants use Firestore generated ids, my FavoriteRestaurant documents would be somewhat lexicographically close as there would be clumps of FavoriteRestaurant documents with a common beginning to their ids. How likely will this structure be at risk of hotspots? Also to give a use case for why these predeterminable ids would be useful is handling transactions over my FavoriteRestaurant documents for something like "store credit". What would be your advice on using composite ids and trying to handle this use case?
Very informative video and thanks for keeping the content up-to-date. Love the discussion around best practices too because these things can be hard to find!
Our pleasure!
Very nice video! Important concepts about how and when to apply which data modelling approach. I totally agree that we as developers need to know what data will be used in the application and how it's gonna be used. Also what kind of queries are inside the gather requirements for the app. Once you know what data you will need to retrieve and how often you will do it, then it will more clear about what model to use.
My advice for Firestore data structure: Use both, remember if you have a hammer not everything becomes nails (this phrase it's better in spanish XD). But an interesting point to notice it's implementation and mantainance time, with top level collections tends to be easier and faster for me, specially if you reuse components in Angular.
Edit: This becomed more useful when Firebase introduced 'array-contains' queries
do you mean use both subcollections and top level collections?
Hope you do How to Structure Your Data Part 2. This one is great. I'd like to see coverage of using local storage IndexedDB etc with Firestore, Performance and displaying results, very large versus very small query results. Data Structure is such a critical part of app development.
Please make more videos on this series; they're all so useful!
These videos are honestly such a treasure. They're fun, informative and easy to understand. There's a huge problem with the captions once Todd from 2019 appears. Not cool for people who need the captions. Still, 7.9/10, would recommend.
Great video, so helpful in getting acquainted with structuring data in firestore. Question about your final example on using a top level collection of favorites.
What are the upsides/drawbacks of tweaking your stucture by replacing the userid field on each top level favorite with an array of all the userids that favorites that restaurant (so each favorites restaurant appears in only 1 document with an array of users who have favorited it contained within?)
I like the top level collections solution brought up at the end. That said, if I am understanding this correctly, by introducing denormalized data and using cloud functions to update all copies of that data when the original data changes, this means more data write operations that we'll be charged - one write per copy. Right?
Am I the only one who has mis-aligned captions & subscribe / watch more content at the end of the video. I'm guessing it's due to the "Todd from 2019" segment being added after the fact. It's like this in many of the videos in this series.
This is a great series of videos! Clear, fun and very informative. Thanks!
Great video, love it! At 08:45, I like the solution, but how can you query "get all restaurants where userID_2852 is a key in the private_data.roles map?" I feel this is a very common user case, but don't quite get how to do it. Would really appreciate your help here!
#askfirebase Thank you for this great series, I have a model like the following:
Collection: Place
String name
String description
Int numberOfVisits
and so on
Q1: for the first two parameters ‘ *name and description* ’ I need to make it *multilingual* what is the best structure to make it so while keeping minimal data downloaded and the minimal number of queries?
Q2: you're talking about having a sub-collection for reviews, but what about having a third collection for the relation between users-resturants-reviews ?
Best,
Yusuf
Hey Todd, thanks for the awesome tutorial. In my application I have list of users and all the pages that they own. There are minimal page queries that span multiple users, so following your recommendation, can I go with storing pages as a sub collection to users? just wondering whether the fact this its being 'User' object would have any impact on the structure?
Do the queries at 11:05, querying documents where a property of a map exists, still work with Firebase 8+ ? I cannot get them to work.
One thing I'm still trying to figure out is best practice for displaying the collection of "restaurants" that a user can browse. Let's say there are 20 restaurants, first option would be to query this collection every time the user browses (20 queries per browse) while a second option would be to query a document containing the list of restaurants and then just query the restaurants that they select (1 query for the list, then 1-5 queries for the restaurants). I would love to hear other's opinions!
I have a question... The example at 13:23, when we get the Favorite Restaurants, we're getting all the document, including the user_ids that have favorited that specific restaurant, what happens if a LOT of users have that restaurant as a favorite one, doesn't it exceed the indexing limit? and also isn't it useless to get all the users that have favotired that restaurant, when that field is only to query information?
This tutorial is so good, even you are not using firebase still a good video about how to structuring data and designing system
I realy love your turorials. Thanks too much.
Thank you Google! So elegant, Firebase is just awesome tbh.
Thank you for this video. I am new to development and often doubt how I want to do things. Watching this reminds me that that there is often no one best way to do things.
What if you are doing the 'inverse' of a favorite, such as a 'done' flag to filter out completed items in a task list? Why are there no inverted logic queries in firestore to support something like this? Ex: an array-not-contains query. Even based on the earlier video where it was explained that queries use a binary search on indices to produce a contiguous sequence of results I don't understand. The inverse of that is simply the concatenation of the two contiguous sequences before and after.
umm current day Todd, you're good to keep this up-to-date but you forget to update the subtitle too, not a big deal but sometimes it help me to better understanding
Denormalisation is against every rule I have learned in most db specially sql. I understand this is nosql so I have no other option then get used to it which I guess will be the hardest part at structuring db in firestore. What a mess.. :o
It's a trade of read-performance against maintainability. Firestore acts more like a fast cache than a real database imo
Should my single document contain only one chat message or an array of all chat messages ?
Thanks for this amazing video and for the whole series! it helped me a lot, looking forward to the next video about security rules.
Yes yes agreed XD
So when you have a N-N relationship like the link "favorite" between Restaurants-Users, you create a new collection that contains the id of the restaurant, the id of the user and the favorite data.
You are just normalizing the unnomalized data like one do in SQL with tables instead of collection ?
Thanks for this tutorials, the real life examples are great and helps to understand everything in a simple way
♥️♥️
If we have a Users and a FavoritedRestaurants collection, where FavoritedRestaurants has user_id and restaurant_id, don't we end up with TONS of duplicate data? Imagine 1000 users favorite the same restaurant, then we have the same restaurant snippet in 1000 different documents. This seems like an undesirable amount data duplication. Is this a right approach to do things?
Okay after watching more videos I see that duplicate data is a thing in NoSQL and it's not so terrible as in SQL
This is so insightful Todd, thank you 🙏
Love the crash course vibes
This was incredibly helpful! Much appreciated, will be watching again.
I love the videos, just want to point out that the subtitles are not updated for Todd from 2019. Thanks!
OK so one of the rules concerning arrays was that you cannot reference elements by index. So assuming that one chooses to store favourites using the structure at 12:10, what would you do if you say, wanted to remove (i.e. "un-star") an item at the second position in your list? You cannot simply delete the item at index [1], since that is not supported. Would you instead need to instruct the database to simply replace the list with the new one constructed on the client side? Or instead, do the array operations support edit-by-value, e.g. "delete item that matches 'rest_5153'", and by virtue of real-time synchronisation, the item removed in my UI is indeed the second item, since one would assume the ordering is synchronised as well?
Can we say, in this example making multiple top level storage will be more efficient than storing data as the snippets or sub-level?
Great video. thank you! So much useful information and it's so interesting to watch at the same time!
Single query required many times to fetch collections and sub-collections together. Is there any plan in near future?
Firebase, React, Material UI and Typescript is awesome!
Love that there's no limit to the number of documents you can have.
Tod, can u explain about using cloud function in Cloud Firestore ? #AskFirebase
Great video! I'm considering using GraphQL with Apollo on the client side and was wondering if it's a good method for fetching data. My stack is React | Apollo Client | Apollo Server | Firestore. Thanks for the help :)
Always glad to see Todd from 2019.
all these examples are easily resolved with relational db. remind me again, why use nosql? also in the proposed solution you present separate options of use, but what if you need this and also that?
This video made me felt NoSQL has so many issues. The more I watched the more awkward I felt. This felt to me like a video on how bad NoSQL is
What if my App/Web is multi language? How do I store a list of Types of restaurants? Should I use Remote Config and store only the Ids in Firestore? Or should I create a different structure in Firestore for that?
best teacher ever!!!
Last winning structure. What if another user adds the same restaurant as a favorite? Does it not mean that the user_id field will be overwritten?
Hi, I must say I love your videos. I'm using a Firestore database and building an app that contains users and clubs, and a user can belong to multiple clubs (ie a club can have multiple users) and a user can be an admin of a club. So a club user that is an admin should be able to see and manage the users of a club, and a user can join clubs and see the details of the clubs etc. I come from a relational database background so naturally I created three top-level collections - clubs (containing club info), users (containing user info), and users_clubs, just containing the combination of clubid and userid and an isadmin indicator (eventually i will proabably want to add more roles/permissions on a user_club level). I'm wondering if in your opinion this is the best option or should I add a subcollection of clubs under users to list the clubs they belong to, or should I add clubs as a map to users (with clubid and role information)? I realize there's no one correct answer to this question but I am trying to minimize cost in terms of storage and queries. Thanks!
video and subtitles don't match after insert fragment from 2019(
Thanks, Great video. bring on the much needed security rules video pls!
I am a miser when it comes to like a video.. But you are gonna get my like.. Keep the good work up.
Anyone using Firestore must watch this video before start planning data structure!
In firestore, can we add new json object in the same document instead of creating a new document in the collection every time user adds as review? If I create separate document for each .add json object, I would have 1000 document reads per day per user which would be expensive.
What would be the best optimum solution?
14:07 - talking to your imaginary friends eh? I thought I was the only one....
Where can I find the video about Cloud functions you mention? Interested to see how to use Cloud functions to keep duplicate data up-to-date across multiple documents.
I think that video only exists in the future (for now). Sounds like one of these on security is next, then maybe functions after that? Not sure of the team's roadmap for this series, but Todd has alluded to functions being the topic of an upcoming video a couple of times. (Someone please correct me if the functions video is already out and I've just missed it.)
Hi the transcript and captions seems to be off! it seems to be lagging behind
at last, if we create a separate user collection and favoriteRestaurants collection. If I change the restaurant address, I have to change all documents that match the same restaurant's id in favoriteRestaurants collection. And I think it's not a good way.
denormalized is the best. memory save is from times where memory was hard to have
Problem with subtitles from 2:37 :(
are you find a solution for subtitles problem?
I solved the problem with selecting second choice in CC (English auto-generated)
Great, broad and deep; with many considerations taken into account.
love this tutorial! besides that, just wanted to let you know that the captions are not synced due to Todd 2019 intteruptions 😆
Guys, @Firebase
How to use reference field here?
For example, I have Posts and Users collections.
in Posts, reference field is Author, link to user: users/userid
When get the Post details, how to get the user details from reference without using another query like: doc(db, "users", userid). I do not want to consume 2 queries for that. Suppose reference field can return the user data in get Post query too.
Really great tutorial!
Thank you! Cheers!
Love this kind of tutorial! greate job!
How to trigger firebase function inside from react via onclick button
"You can't run 2 array contains queries on the same array", I don't really understand why you can't except that arrays are weird. BTW thanks for the video, this is the best one on NoSQL, you're a genius.
The last example: Here is a problem (many-to-many-relations) that NoSQL databases are generally bad at ...[long list of possible solutions] ... the best solution is to use the traditional SQL solution of storing the many-to-many relation in a separate table.
Very nice video. Just adding one more use case to the given examples, please help me understand what would be the preferred approach. Now Along with user's favorite restaurants, I miraculously also receive user spending per restaurant. I want a report of individual's average spending per restaurant. And also a restaurant's per user per-month/year/etc.
Very helpful, thanks so much!
why not use custom claims (stored on the user ID token) for role based access management instead of an array of UIDs?
firebase.google.com/docs/auth/admin/custom-claims
Coming from SQL this feels kind of hacky but i guess it's a different way of thinking. Great video nonetheless; you explained it really well.
This is very very well done
Great video!
Hello Todd,
I am developing a flutter app and I have this "news" collection with documents pointing to each article. These documents have the field "viewed" which is basically an array that contains the uids of all users who have viewed the news article.
I want to list the unread news to the user so I was thinking of setting up a streambuilder to query for something like
.where("viewed", "!arrayContains", "uid")
However, this is not going to work for obvious reasons.
Could you please shed some light on the possibilities of a data model so that I can look into setting up a streambuilder that can populate a listview of unread news for the user?
I have the top level "users" collection with essential user details.
do queries support ! operator
Excelent video, right now I'm building an web app an this is extremely helpful to get your thoughts in the right direction
please upload all series
Collection group queries
Deleting a subcollection from client
Regex test search in string field
These 3 things and firestore will be the dream back end for all app devs :)
I totally agree with you. it will become very very powerful
Yeah but then google can't earn money lolz
Really good tutorial.
Isn't putting everything in top level collections like using a SQL-like database structure?
kinda, except you duplicate data very often
12:28 - am I missing something? How would we create the FavoriteRestaurants top-level collection? One restaurant can by 'favorited' by multiple users, so how do you set the "user-id" in each document of this array?
Hi @@ToddKerpelmanCorp! First of all, excellent videos series, very helpful! I have one doubt regarding the favorites use-case and the top-level collection approach. Is there any way to avoid someone creating duplicated documents? I can only think of creating a composite key for each document, something like restId_userId, or having a cloud function monitoring each creation in order to eliminate any duplicates. Are any of these the recommended way to accomplish this? Thanks
TLDW; . . . top level collections are good.
@@ToddKerpelmanCorp Cool, thanks for caring enough to cite those caveats. I do have a suggestion for your consideration. Since so many developers are going to face this design problem when working with Firebase, it might be a good idea to make a decision tree? Default to top level, then ask a series of questions, "Does your application have intensive write requirements?" Etc. That would enable a developer to reach a pretty good design conclusion if they understand their own requirements. Otherwise, to get to the right answer, they need to understand both their application requirements as well as all the idiosyncrasies of a database that they never used.
@@ToddKerpelmanCorp So what do I do with such data? For example some kind of event stream. As all fields are indexed, just having a random ID isn't enough to prevent this limitation. Maybe I should batch multiple events into documents?
Hey Todd :) with the new collection groups, does that mean sub collection could actually be preferred now?
@Todd Kerpelman +1
Its really helpful with this kind of tutorials, why not do an updated version in firestore about event and members etc that you did for realtime database!
I have a question on how to update the snippet reviews on 5:29
Can anyone tell me why you just wouldn't store *only* the restaurantId in the favourites array of the user collection? I'm pretty sure you could just query all the restaurantId's in the users favourites and pull all the data that way.
Subtitles is not synched after Todd from 2019 shows up
Just perfect! Big thumb up!
How do we store the array(favorites) of objects shown at 10:30 in Android?
Thanks Todd, this advice is right on the point for me 👍
Now I am curious for the cloud function video :D
Can someone explain to me how 2019 Todd appears in a video published in 2018?
implementing likes or rating for favorites restaurant will be really expensive if it has been favorited by a lot of people. Assuming 40 million people favorite a restaurant it means you will have to perform a write for each person who favorited a restaurant. Is there a cheaper alternative.
did you find any answer? I am looking to do this myself.
Thank you!!! Very helpful.
the winner 4 will charge (billed) me twice?
This guy know how to communicate knowledge