Fireship · @fireship

Collection Group Queries in Firestore

Uploaded May 21, 2019 · Length 8:19

Collection Group Queries in Firestore

Summary

Firestore collection group queries explained with a reddit-style comments example using Angular.

  • firebase firestore collection group queries
  • angular firestore tutorial
  • reddit style comments in firestore
  • nested comments in firestore
  • collection group query example

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    [Music]

  2. 0:06

    one of the most frustrating things about

  3. 0:08

    firestore is modeling your data as a

  4. 0:10

    hierarchy of subcollections because even

  5. 0:12

    though this is a very intuitive data

  6. 0:14

    model that represents relationships very

  7. 0:15

    well it's not very practical because you

  8. 0:17

    can't join these collections together to

  9. 0:19

    query them at the same time well at

  10. 0:21

    least that was the case until now

  11. 0:23

    firebase just recently released a new

  12. 0:24

    feature called collection group queries

  13. 0:26

    in today's video you'll learn how this

  14. 0:28

    new feature makes working with

  15. 0:29

    subcollections a lot more intuitive and

  16. 0:31

    we'll put this new feature to use to

  17. 0:33

    make a deeply nested threaded comments

  18. 0:34

    feature with angular if you're new here

  19. 0:36

    like and subscribe and you can grab the

  20. 0:38

    full source code from fire ship IO and

  21. 0:40

    if you're building a product with fire

  22. 0:41

    store consider becoming a pro member to

  23. 0:43

    get access to the data modeling course

  24. 0:45

    let's first take a look at the problem

  25. 0:47

    that we can now solve with a collection

  26. 0:48

    group query let's consider the very

  27. 0:50

    typical data model of blog post comments

  28. 0:52

    users can have many comments post

  29. 0:54

    connect many comments and a comment

  30. 0:55

    belongs to a post and a user will have

  31. 0:58

    at least two screens in the app that we

  32. 0:59

    want to fill with data most commonly

  33. 1:01

    we'll want to show all of the comments

  34. 1:03

    associated to a given post but we might

  35. 1:05

    also have something like a user activity

  36. 1:07

    feed that queries all the most recent

  37. 1:08

    comments from that user read it would be

  38. 1:10

    a perfect example of this and reddit

  39. 1:12

    also allows users to respond to comments

  40. 1:14

    so you can get these deeply nested

  41. 1:16

    threads of comments which is another

  42. 1:17

    problem that is much easier to solve

  43. 1:19

    with collection group queries now if

  44. 1:21

    you've been using firestore for a while

  45. 1:23

    the most likely way that you modeled a

  46. 1:24

    relationship like this was to create a

  47. 1:26

    root collection of comments and then

  48. 1:28

    save a post ID as well as a user ID on

  49. 1:30

    each individual comment the reason you

  50. 1:32

    wouldn't make comments a sub collection

  51. 1:33

    of a post is because you then wouldn't

  52. 1:35

    be able to query all of the comments

  53. 1:37

    created by an individual user and that's

  54. 1:39

    the exact problem that collection group

  55. 1:40

    solves because it allows you to group

  56. 1:42

    all the collections in the database

  57. 1:44

    based on a shared name or ID and it

  58. 1:46

    doesn't matter how deeply nested these

  59. 1:48

    collections are they can be nested up to

  60. 1:50

    a hundred levels deep and still be

  61. 1:51

    included in a collection group query and

  62. 1:53

    that's extremely useful when you have a

  63. 1:55

    complex hierarchy of data like you would

  64. 1:57

    with reddit comments in this example

  65. 1:59

    here we have a collection of posts and