Check out a free preview of the full API Design in Node.js, v3 course:
The "Data Modeling with MongoDB" Lesson is part of the full, API Design in Node.js, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Scott discusses why it's important to introduce Schemas to the schemaless database.

Get Unlimited Access Now

Transcript from the "Data Modeling with MongoDB" Lesson

[00:00:00]
>> Scott Moss: Data Modeling with MongoDB. So this will just be more of a reference thing for you all and how to do things with Mongo than it is completely new different data bases. So if you've never used a database before, I'm not gonna go into extreme detail about how databases work and all that stuff.

[00:00:18] We have another course for that, intro to MongoDB that goes a little further into that. But I will be talking about how to use Mongo and Mongoose in this course. So if you want more detail about just the database in general, definitely check out that course. It's mostly about data modeling and not so much working with the database.

[00:00:37] Cool, so we talk about Mongo being a document store where it's schemaless. So basically, you can store whatever you want in there and it'll save it. But I said that's a really bad idea and we'll talk about it later. Well, this is now. [LAUGH] We don't wanna do that.

[00:00:52] So we definitely want to use schemas for our schemaless DB. So what does that mean and why do I always use Postgress instead? Well, you can. You can use prosgrez if you want. But I decided to use Mongo for this exercise because it's a lot easier to get set up.

[00:01:06] But yeah, you should always use a schema for you models, and Mongoose makes it easy to create those schemas, and that's just my tool of choice. So MongoDB is a schemaless document store, you should always use a schemaless, otherwise you will go crazy. I've actually worked with organizations before that they switched over from like to some relational database, and then they used Mongo because it was schemaless.

[00:01:30] And they went with that and it just threw everything in the database. And then at some point, you're gonna have to check the data. At some point, you're gonna have to do that. So guess where they decided to do it? On the front end. So on the front end, it's literally just validating all the data that comes back from an API and it is the worst place to do it.

[00:01:47] I mean, the front end was just full of errors. Full of errors, there were no tests, it was really bad. So that's what happens if you don't have a schema. At some point, you're gonna have to validate that data. You better validate it before it goes in the database.

[00:01:59] Otherwise every single client that use that data is gonna have to do some type of validation in order to work. So definitely use a schema. MongoDB has added support for creating schema, so mongoose is a separate package than MongoDB. For MongoDB, the JavaScript client has added support for schemas, like this year.

[00:02:19] So you could use just the MongoDB client to create schemas and the way they do it is a little different than the way Mongoose does it. But they too have created support for schemas although they are a schemas database. So even they're saying, like yeah they should probably use some schemas here.

[00:02:34] So you can use that if you want, but Mongoose is still way better. Just way better.
>> Scott Moss: So we can create the models for each REST resource we want, and then we can expose it to the API. So what does that mean? So if we go over to our resources here, for instance, let's look at the user resource, you can see I have controller model router, so I click on model,

[00:03:00]
>> Scott Moss: You can see I have a schema here that describes what fields this user resource is gonna have on it. And then I use that schema to create a model. And the model is the JavaScript representation of that database resource that we're gonna use, right? This is how we interact with the users and the databases by using this user model.

[00:03:23] So that's how Mongoose kind of works with schemas and models. You create a schema and that schema is used to create a model. And that model holds instructions for that schema so you can use it in JavaScript.