Fireship · @fireship

Faster Firestore via Data Aggregation

Uploaded October 31, 2017 · Length 9:28

Faster Firestore via Data Aggregation

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.

  1. 0:00

    are you interested in retrieving data

  2. 0:01

    from the firestore database in a way

  3. 0:03

    that's both faster and more

  4. 0:04

    cost-effective if so you're watching the

  5. 0:07

    right video today I'm going to show you

  6. 0:09

    how to use firebase cloud functions to

  7. 0:10

    automatically aggregate or add up

  8. 0:13

    information from a sub collection and

  9. 0:15

    then write it to its parent document

  10. 0:16

    take for example a post document that

  11. 0:19

    has a comment sub collection nested

  12. 0:21

    under it whenever a new comment is

  13. 0:24

    created it's going to trigger a cloud

  14. 0:25

    function that will read this comment sub

  15. 0:28

    collection then add up the comment count

  16. 0:30

    and the five most recent comments and

  17. 0:32

    then duplicate them on the post document

  18. 0:34

    on a no sequel document database like

  19. 0:37

    firestore duplication is perfectly

  20. 0:39

    acceptable imagine if we had a post with

  21. 0:41

    a hundred comments we'd have to read all

  22. 0:43

    those comments to get the total every

  23. 0:44

    time someone viewed a post not only is

  24. 0:46

    that slower for the end user but it's

  25. 0:48

    also going to cost a lot more money as

  26. 0:49

    your app scales up my goal today is to

  27. 0:52

    show you how to solve this problem using

  28. 0:53

    back-end data aggregation if you're new

  29. 0:56

    here make sure to subscribe and you can

  30. 0:58

    grab the full source code from angular

  31. 1:00

    firebase com I'm going to be starting

  32. 1:02

    from a brand new angular app so run ng

  33. 1:04

    new it's only dependency is angular fire

  34. 1:06

    too and you can follow the setup

  35. 1:08

    instructions for that on the main repo

  36. 1:10

    if you're not using angular you can skip

  37. 1:12

    ahead to the cloud function code which

  38. 1:13

    is just written in plain JavaScript the

  39. 1:16

    first thing I'll show you is our

  40. 1:17

    underlying data we have a post

  41. 1:19

    collection and then this test post under

  42. 1:21

    it then you can see we have a recent

  43. 1:23

    comments array here which is the five

  44. 1:25

    most recent comments from potentially

  45. 1:27

    hundreds or even thousands of comments

  46. 1:28

    in the subcollection having this data on

  47. 1:31

    the parent dock is extremely useful if

  48. 1:33

    you loop through a list of posts and you

  49. 1:34

    just want to show a quick preview of the

  50. 1:36

    most recent comment as well as the total

  51. 1:38

    comment count the comment sub collection

  52. 1:40

    is very simple it just has a content

  53. 1:43

    property and a created at timestamp at

  54. 1:46

    this point I want to point out that this

  55. 1:47

    is not suitable for every situation it's

  56. 1:50

    ideal when you have documents that

  57. 1:51

    receive many reads but not very many

  58. 1:53

    writes a good example would be Yelp

  59. 1:56

    where many users want to see the reviews

  60. 1:58

    for a location but only a small