Fireship · @fireship
Model Relational Data in Firestore NoSQL
Uploaded February 9, 2018 · Length 9:13
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.
- 0:00
Modeling relational data in a NoSQL database like Firestore can be a challenge, especially
- 0:07
if you come from a SQL background because you'll have to unlearn many of the principles
- 0:11
that you already know.
- 0:12
My goal today is to show you a handful of techniques that you can use to model data
- 0:15
relationships, such as one-to-one, one-to-many, and many-to-many.
- 0:20
Because NoSQL is schema-less, we have the flexibility to model these relationships in a variety
- 0:25
of different ways.
- 0:26
I'm going to show you how to build these data models in the Firestore console, and we'll
- 0:29
talk about the trade-offs for each technique.
- 0:32
If you're new here, make sure to like and subscribe, and follow along with the source
- 0:35
code at angularfirebase.com.
- 0:39
Firestore is a NoSQL, document-oriented database, similar to CouchDB and MongoDB if you're
- 0:45
familiar with those.
- 0:46
Instead of storing data with a bunch of columns and rows like you would with a traditional
- 0:50
SQL database, you store data as collections of documents.
- 0:55
Collections don't store any data of their own, they're just containers to hold multiple
- 0:58
documents that you can query.
- 1:00
And a document itself can just be thought of as a JSON object.
- 1:04
In order to keep your queries efficient, it's important that you make your collections large
- 1:09
and your documents small.
- 1:10
In fact, documents need to be at least 1 megabyte or smaller.
- 1:14
But a collection can hold millions of documents and still be queried efficiently.
- 1:19
Firestore gives you three fundamental ways to model your data.
- 1:22
The first way would be a root collection, which is just a collection of documents in
- 1:26
the root of your database.
- 1:28
The second option is to embed data directly on the document itself.
- 1:32
It behaves very similar to JSON, so we can save objects, arrays, and other data types
- 1:37
on the document.
- 1:38
It just needs to be relatively small, again limited to 1 megabyte.
- 1:43
Then our third option is to nest a sub-collection on a document.
- 1:47
This allows us to scope collections to a parent document, creating a hierarchy structure.
- 1:52
Now that we know the fundamentals, let's jump into the Firestore console and start modeling
- 1:56
some data.
- 1:58
The first thing we'll do is create a root collection called Users that maintains a document


