Fireship · @fireship
Collection Group Queries in Firestore
Uploaded May 21, 2019 · Length 8:19
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.
- 0:00
[Music]
- 0:06
one of the most frustrating things about
- 0:08
firestore is modeling your data as a
- 0:10
hierarchy of subcollections because even
- 0:12
though this is a very intuitive data
- 0:14
model that represents relationships very
- 0:15
well it's not very practical because you
- 0:17
can't join these collections together to
- 0:19
query them at the same time well at
- 0:21
least that was the case until now
- 0:23
firebase just recently released a new
- 0:24
feature called collection group queries
- 0:26
in today's video you'll learn how this
- 0:28
new feature makes working with
- 0:29
subcollections a lot more intuitive and
- 0:31
we'll put this new feature to use to
- 0:33
make a deeply nested threaded comments
- 0:34
feature with angular if you're new here
- 0:36
like and subscribe and you can grab the
- 0:38
full source code from fire ship IO and
- 0:40
if you're building a product with fire
- 0:41
store consider becoming a pro member to
- 0:43
get access to the data modeling course
- 0:45
let's first take a look at the problem
- 0:47
that we can now solve with a collection
- 0:48
group query let's consider the very
- 0:50
typical data model of blog post comments
- 0:52
users can have many comments post
- 0:54
connect many comments and a comment
- 0:55
belongs to a post and a user will have
- 0:58
at least two screens in the app that we
- 0:59
want to fill with data most commonly
- 1:01
we'll want to show all of the comments
- 1:03
associated to a given post but we might
- 1:05
also have something like a user activity
- 1:07
feed that queries all the most recent
- 1:08
comments from that user read it would be
- 1:10
a perfect example of this and reddit
- 1:12
also allows users to respond to comments
- 1:14
so you can get these deeply nested
- 1:16
threads of comments which is another
- 1:17
problem that is much easier to solve
- 1:19
with collection group queries now if
- 1:21
you've been using firestore for a while
- 1:23
the most likely way that you modeled a
- 1:24
relationship like this was to create a
- 1:26
root collection of comments and then
- 1:28
save a post ID as well as a user ID on
- 1:30
each individual comment the reason you
- 1:32
wouldn't make comments a sub collection
- 1:33
of a post is because you then wouldn't
- 1:35
be able to query all of the comments
- 1:37
created by an individual user and that's
- 1:39
the exact problem that collection group
- 1:40
solves because it allows you to group
- 1:42
all the collections in the database
- 1:44
based on a shared name or ID and it
- 1:46
doesn't matter how deeply nested these
- 1:48
collections are they can be nested up to
- 1:50
a hundred levels deep and still be
- 1:51
included in a collection group query and
- 1:53
that's extremely useful when you have a
- 1:55
complex hierarchy of data like you would
- 1:57
with reddit comments in this example
- 1:59
here we have a collection of posts and


