Advertisement
English transcript
OK welcome back.
This week, we're going to talk about MongoDB schema design.
If you're coming from the world of relational databases,
you know that there's a best, ideal way to design your
schema, which is to keep it in the third normal form.
Now, you can keep data in the third normal form in MongoDB.
And we'll talk a little bit about the third normal form
next lesson.
But in Mongo, it turns out that it's more important to
keep the data in a way that's conducive to the application
using the data.
So you think about your application data patterns.
You think about what pieces of data are used together, what
pieces of data are used mostly read-only, what pieces of data
are written all the time.
And then we're going to organize our data within
MongoDB to specifically suit the
application data access patterns.
And that's a bit different from the world of relational,
where instead, you try to keep the data in a way that's
agnostic to the application.
So let's go over some of the basic facts about MongoDB just
to remind you of them as we talk about schema design.
The first is that MongoDB supports rich documents.
And by rich documents, I mean that it's not
just tabular data.
In MongoDB, you can store an array of items, or a value for
a certain key can be an entire other document.
And this is going to allow us to pre-join
data for fast access.
And that's important because, as you know, MongoDB doesn't
support joins directly inside the kernel.
Instead, if you want to do a join, you're going to have to
join in the application itself.
And of course, the reason we do this is because that joins
are very hard to scale.
And we want the system to be high performance.
So as a result of this, you need to think ahead of time
about what data you want to use together with other data.
And if it's possible, you might want to embed it
directly within a document.
And we'll talk about how you do that.
The next is that there are no constraints.
Now, if you come from the world of relational, you might
remember that you could have a foreign key constraint and ask
that a attribute of one row in a table would form a foreign
key into another table.
And we don't have that in MongoDB.
But it's going to turn it's not as important as you might
think because of embedding.
Embedding is going to make that a little less important
than it would otherwise be.
We're going to think about atomic operations.
We don't support transactions.
But we do support atomic operations within MongoDB
within one document.
So we're going to think about how to organize our data to
support atomic operations if we need them within our
application.
And the final thing is that there is no declared schema
within MongoDB.
But there's a pretty good chance that your application
is going to have a schema.
And by have a schema, I mean that every single document in
a particular collection is probably going to have a
pretty similar structure.
And there might be some small changes to that structure,
depending on different versions of your application.
But mostly, each document in the collection's going to have
a similar structure.
And even though it's not declared ahead of time, it's
important to think about that structure so that the data
schema itself supports all the different features of your
application.
So let's have a first quiz.
The first quiz for schema design is what's the single
most important factor in designing your application
schema within MongoDB?
Is it making the design extensible, making it easy to
read by a human, matching the data access patterns of your
application, or keeping the data in third normal form?
And just check one of those.
This week, we're going to talk about MongoDB schema design.
If you're coming from the world of relational databases,
you know that there's a best, ideal way to design your
schema, which is to keep it in the third normal form.
Now, you can keep data in the third normal form in MongoDB.
And we'll talk a little bit about the third normal form
next lesson.
But in Mongo, it turns out that it's more important to
keep the data in a way that's conducive to the application
using the data.
So you think about your application data patterns.
You think about what pieces of data are used together, what
pieces of data are used mostly read-only, what pieces of data
are written all the time.
And then we're going to organize our data within
MongoDB to specifically suit the
application data access patterns.
And that's a bit different from the world of relational,
where instead, you try to keep the data in a way that's
agnostic to the application.
So let's go over some of the basic facts about MongoDB just
to remind you of them as we talk about schema design.
The first is that MongoDB supports rich documents.
And by rich documents, I mean that it's not
just tabular data.
In MongoDB, you can store an array of items, or a value for
a certain key can be an entire other document.
And this is going to allow us to pre-join
data for fast access.
And that's important because, as you know, MongoDB doesn't
support joins directly inside the kernel.
Instead, if you want to do a join, you're going to have to
join in the application itself.
And of course, the reason we do this is because that joins
are very hard to scale.
And we want the system to be high performance.
So as a result of this, you need to think ahead of time
about what data you want to use together with other data.
And if it's possible, you might want to embed it
directly within a document.
And we'll talk about how you do that.
The next is that there are no constraints.
Now, if you come from the world of relational, you might
remember that you could have a foreign key constraint and ask
that a attribute of one row in a table would form a foreign
key into another table.
And we don't have that in MongoDB.
But it's going to turn it's not as important as you might
think because of embedding.
Embedding is going to make that a little less important
than it would otherwise be.
We're going to think about atomic operations.
We don't support transactions.
But we do support atomic operations within MongoDB
within one document.
So we're going to think about how to organize our data to
support atomic operations if we need them within our
application.
And the final thing is that there is no declared schema
within MongoDB.
But there's a pretty good chance that your application
is going to have a schema.
And by have a schema, I mean that every single document in
a particular collection is probably going to have a
pretty similar structure.
And there might be some small changes to that structure,
depending on different versions of your application.
But mostly, each document in the collection's going to have
a similar structure.
And even though it's not declared ahead of time, it's
important to think about that structure so that the data
schema itself supports all the different features of your
application.
So let's have a first quiz.
The first quiz for schema design is what's the single
most important factor in designing your application
schema within MongoDB?
Is it making the design extensible, making it easy to
read by a human, matching the data access patterns of your
application, or keeping the data in third normal form?
And just check one of those.