Fireship · @fireship
Faster Firestore via Data Aggregation
Uploaded October 31, 2017 · Length 9:28
Summary
Use firebase cloud functions to automatically aggregate comments from a subcollection in Firestore and write them to the parent document for faster and more cost-effective data retrieval.
- firebase cloud functions
- firestore data aggregation
- subcollection comments
- parent document aggregation
- cost-effective data retrieval
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
are you interested in retrieving data
- 0:01
from the firestore database in a way
- 0:03
that's both faster and more
- 0:04
cost-effective if so you're watching the
- 0:07
right video today I'm going to show you
- 0:09
how to use firebase cloud functions to
- 0:10
automatically aggregate or add up
- 0:13
information from a sub collection and
- 0:15
then write it to its parent document
- 0:16
take for example a post document that
- 0:19
has a comment sub collection nested
- 0:21
under it whenever a new comment is
- 0:24
created it's going to trigger a cloud
- 0:25
function that will read this comment sub
- 0:28
collection then add up the comment count
- 0:30
and the five most recent comments and
- 0:32
then duplicate them on the post document
- 0:34
on a no sequel document database like
- 0:37
firestore duplication is perfectly
- 0:39
acceptable imagine if we had a post with
- 0:41
a hundred comments we'd have to read all
- 0:43
those comments to get the total every
- 0:44
time someone viewed a post not only is
- 0:46
that slower for the end user but it's
- 0:48
also going to cost a lot more money as
- 0:49
your app scales up my goal today is to
- 0:52
show you how to solve this problem using
- 0:53
back-end data aggregation if you're new
- 0:56
here make sure to subscribe and you can
- 0:58
grab the full source code from angular
- 1:00
firebase com I'm going to be starting
- 1:02
from a brand new angular app so run ng
- 1:04
new it's only dependency is angular fire
- 1:06
too and you can follow the setup
- 1:08
instructions for that on the main repo
- 1:10
if you're not using angular you can skip
- 1:12
ahead to the cloud function code which
- 1:13
is just written in plain JavaScript the
- 1:16
first thing I'll show you is our
- 1:17
underlying data we have a post
- 1:19
collection and then this test post under
- 1:21
it then you can see we have a recent
- 1:23
comments array here which is the five
- 1:25
most recent comments from potentially
- 1:27
hundreds or even thousands of comments
- 1:28
in the subcollection having this data on
- 1:31
the parent dock is extremely useful if
- 1:33
you loop through a list of posts and you
- 1:34
just want to show a quick preview of the
- 1:36
most recent comment as well as the total
- 1:38
comment count the comment sub collection
- 1:40
is very simple it just has a content
- 1:43
property and a created at timestamp at
- 1:46
this point I want to point out that this
- 1:47
is not suitable for every situation it's
- 1:50
ideal when you have documents that
- 1:51
receive many reads but not very many
- 1:53
writes a good example would be Yelp
- 1:56
where many users want to see the reviews
- 1:58
for a location but only a small


