Firestore Data Modeling - Five Cool Techniques | Summary and Q&A

TL;DR
Learn about five different ways to structure and query data in Firestore, including modeling relational data, filtering by categories, using composite strings for hierarchical structures, joining data from different collections, and building a social media follower feed.
Key Insights
- π Building an app with Firestore requires careful data structure planning to optimize performance and minimize queries.
- π΅ Firestore can effectively handle relational data modeling by rethinking the approach.
- π€± Techniques covered in the video include querying based on user mentions, filtering and querying by multiple categories, using composite strings for hierarchical structures, joining data from different collections, and building a follower feed system.
- 𧑠Firestore provides operators like array contains, order by, and range operators to facilitate querying and filtering data efficiently.
Transcript
Read and summarize the transcript of this video on Glasp Reader (beta).
Questions & Answers
Q: How can Firestore handle querying based on user mentions in posts?
Firestore allows storing an array of mentioned usernames on the post document as a way of managing the many-to-many relationship. By querying the post collection with an array contains operator and the user ID, all tweets mentioning a particular user can be obtained. A cloud function can be set up to trigger push notifications or emails when a user is mentioned.
Q: Can Firestore handle filtering and querying based on multiple categories and allowing for exclusion of specific values?
Yes, this can be achieved by using a tags collection to hold tag-related data and associating these tags with the posts using a map. To filter by multiple tags simultaneously (logical AND), multiple where statements with equal to operators can be chained together. For logical OR queries, individual queries for separate tags can be run concurrently and joined together. The limitation is that only one range operator can be used per query.
Q: How can composite strings be used in Firestore for hierarchical structures like threaded comments?
Composite strings enable efficient traversal in hierarchical structures. By creating a composite key where the elements correspond to the parent documents' IDs, it becomes possible to query specific nodes in the tree. By using range operators in the query (greater than or equal to parent ID and less than or equal to parent ID + a high Unicode character), traversal down the tree can be achieved.
Q: How can Firestore join data from different collections efficiently?
Although denormalization is preferred in Firestore, in cases where embedding data is not practical, data can be joined using multiple read requests sent concurrently. By mapping an array of IDs to document reads and resolving all promises concurrently using Promise.all, the joined data can be retrieved efficiently.
Summary & Key Takeaways
-
Building a successful app requires careful data structure planning, especially with a NoSQL database like Firestore. Modeling data to fit the app's view, minimizing queries, and maximizing performance is crucial.
-
Firestore, contrary to popular belief, can handle relational data modeling effectively by rethinking the approach.
-
Techniques covered in the video include querying based on user mentions in posts, using tags for filtering and querying by multiple categories, using composite strings for hierarchical structures like threaded comments, joining data from different collections, and creating a follower feed system.
Share This Summary π
Explore More Summaries from Fireship π





