Fireship · @fireship

Model Relational Data in Firestore NoSQL

Uploaded February 9, 2018 · Length 9:13

Model Relational Data in Firestore NoSQL

Summary

Learn how to model relational data in Firestore NoSQL database, including one-to-one, one-to-many, and many-to-many relationships.

  • firestore nosql database
  • relational data modeling
  • one-to-one relationships
  • one-to-many relationships
  • many-to-many relationships
  • nosql database design
  • data modeling in firestore

Transcript preview

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

  1. 0:00

    Modeling relational data in a NoSQL database like Firestore can be a challenge, especially

  2. 0:07

    if you come from a SQL background because you'll have to unlearn many of the principles

  3. 0:11

    that you already know.

  4. 0:12

    My goal today is to show you a handful of techniques that you can use to model data

  5. 0:15

    relationships, such as one-to-one, one-to-many, and many-to-many.

  6. 0:20

    Because NoSQL is schema-less, we have the flexibility to model these relationships in a variety

  7. 0:25

    of different ways.

  8. 0:26

    I'm going to show you how to build these data models in the Firestore console, and we'll

  9. 0:29

    talk about the trade-offs for each technique.

  10. 0:32

    If you're new here, make sure to like and subscribe, and follow along with the source

  11. 0:35

    code at angularfirebase.com.

  12. 0:39

    Firestore is a NoSQL, document-oriented database, similar to CouchDB and MongoDB if you're

  13. 0:45

    familiar with those.

  14. 0:46

    Instead of storing data with a bunch of columns and rows like you would with a traditional

  15. 0:50

    SQL database, you store data as collections of documents.

  16. 0:55

    Collections don't store any data of their own, they're just containers to hold multiple

  17. 0:58

    documents that you can query.

  18. 1:00

    And a document itself can just be thought of as a JSON object.

  19. 1:04

    In order to keep your queries efficient, it's important that you make your collections large

  20. 1:09

    and your documents small.

  21. 1:10

    In fact, documents need to be at least 1 megabyte or smaller.

  22. 1:14

    But a collection can hold millions of documents and still be queried efficiently.

  23. 1:19

    Firestore gives you three fundamental ways to model your data.

  24. 1:22

    The first way would be a root collection, which is just a collection of documents in

  25. 1:26

    the root of your database.

  26. 1:28

    The second option is to embed data directly on the document itself.

  27. 1:32

    It behaves very similar to JSON, so we can save objects, arrays, and other data types

  28. 1:37

    on the document.

  29. 1:38

    It just needs to be relatively small, again limited to 1 megabyte.

  30. 1:43

    Then our third option is to nest a sub-collection on a document.

  31. 1:47

    This allows us to scope collections to a parent document, creating a hierarchy structure.

  32. 1:52

    Now that we know the fundamentals, let's jump into the Firestore console and start modeling

  33. 1:56

    some data.

  34. 1:58

    The first thing we'll do is create a root collection called Users that maintains a document