Maps, Arrays and Subcollections, Oh My! | Get to know Cloud Firestore #4

Firebase · 305K views · 2018 · 13:48

Open on YouTube

Databases & storageWeb developmenttutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering Firestore data modeling questions, especially when arguing for subcollections over large arrays or nested maps.

Recommended by 10 people across 10 different months, 2018 to 2024.

What it is

Episode 4 of Firebase's 'Get to Know Cloud Firestore' series, presented by Todd Kerpelman. It compares maps, arrays and subcollections as ways to structure data in Cloud Firestore, covering the pros and cons of each, including limits, querying and concurrency.

What people said

Strong rec egeer · 2024-03 · Stack Overflow · on “Firestore: update specific element of an array”

I highly recommend this video on Firestore Arrays vs Subcollections: Maps, Arrays and Subcollections, Oh My! | Get to know Cloud Firestore #4 [this video] In short, the way that they work, you will always have this race condition sort of problem. The best approach for your use case is probably to just use a subcollection instead of an array. Arrays are really intended to be static objects like flags, and not a collection of dynamic data. You could use a transaction, but you may still have concurrency issues.

Recommends Todd Kerpelman · 2018-09 · Stack Overflow · on “Work around firestore document size limit?”

We actually just released a video on this topic, which might help you out. Or it'll give you even more stuff to overthink. [this video]

Recommends Frank van Puffelen · 2018-09 · Stack Overflow · on “Firebase database retrieving data from comma seperated list”

Firebase Realtime Database doesn't have the equivalent of SQLs IN clause. It also doesn't have a way to find a substring in a value. So the data model you are looking to use, doesn't allow the use-case you want. As usual with NoSQL databases, the solution is to pick a data model that does allow your use-case.. The most likely cause I know for the structure you describe is to associate the child node with a bunch of categories. If that is your case, read my answer here for a proper data structure: Firebase query if child of child contains a value [LINK] This is one of the cases where the new Cloud Firestore database offers better querying support, since it recently added a feature to efficien

Recommends Kqtr · 2018-10 · Stack Overflow · on “How to query for more than one item in an array in Firestore? ("array-contains")”

Thank you so much, Pascal. This seems to be the way to go indeed. For anyone interested, this Firebase official video is really useful to understand arrays, maps and sub-collections, and when to use one or the other: [this video]

Recommends UdeshUK · 2018-11 · Stack Overflow · on “How to query to Firestore sub document”

Currently Firebase Cloud Firestore doesn't support querying with sub collection. You may have to structure your database in way that's possible to query. Store userid in a array in the car document. Use a separate collection to keep the connection between user and cars. You can checkout this video from Firebase. Maps, Arrays and Subcollections, Oh My! | Get to Know Cloud Firestore [this video]

Recommends Frank van Puffelen · 2019-02 · Stack Overflow · on “how to create a compopsite key in cloud firestore for document path that would be changing”

It looks like you have this style of field in your documents: "users": { "uid1": true, "uid3": true, "uid4": true, "me": true } This used to be the recommended way to store a set of values (UIDs in your case), but that is no longer true. In fact, precisely because of the problem you are running into, Firestore now supports using arrays to store such sets of values. The equivalent in your case would be: "users": ["uid1", "uid3", "uid4", "me"] And you can then use the array-contains operation in your query: .where("users", "array-contains", "${curntUser.isNotEmpty?curntUser:'me'}") This operator was added in version 0.8 of FlutterFire [LINK] , and is the solution to your problems. Also see: th

Recommends Kato · 2019-06 · Stack Overflow · on “How can I segregate user data in firebase?”

Todd put together a great series on Firestore [LINK] and it covers database architecture in depth in eposides #3 [this video] and #4 [LINK] .

Recommends Kato · 2019-06 · Stack Overflow · on “Correct way to store an array of custom objects in Cloud Firestore?”

Todd put together a great series on Firestore [LINK] and it covers database architecture in depth in eposides #3 [this video] and #4 [LINK] .

Recommends matthew · 2020-03 · Stack Overflow · on “Advantages of firestore sub-collections”

To answer the original question about efficiency: Querying all people with the family 'Smith' from the people top-level collections really is not any slower than asking for all the people in the 'Smith' family sub-collection . This is explained in the How to Structure Your Data [LINK] episode of the Get to Know Cloud Firestore video series. There are some trade-offs between top-level collections and sub-collections to be aware of. Depending on the specific queries you intend to use you may need to create composite indexes to query top-level collections or collection group indexes to query sub-collections. Both these index types count towards the 200 index exemptions limit [LINK] . These trad

Recommends Rafael Lemos · 2020-03 · Stack Overflow · on “Suggested way to structure Firestore database for deeply nested set of spreadsheet like objects”

From my stand point, there is no obvious answer to your problem and indeed it does depend on what you want to optimize for. However there are a few points that you need to consider on your decision: Documents in Firestore have a limit of 1Mb/Document; Documents in Firestore have a limit of 20000 fields; Queries are shallow, so you don't get data from subcollections on the same query; For considerations 1 and 2, this means that if you choose the design you database to a big document containing everything, even though you said that your app will have lots of data, I doubt that it will be more than the limits mentioned, still, do consider those. Also, how necessary is it to get all the data at