3.3 Creating the Database Schema
While it's true that MongoDB is very flexible and doesn't need a "hard" schema like a relational database, from a software development perspective it makes sense to define what the objects being stored in MongoDB look like. This simply makes the development process easier to understand when we're working with the collections stored in MongoDB. So in this lesson, you'll learn how to use Mongoose to create two schema definitions for your apps.
1.Introduction2 lessons, 07:21
2.Getting Started3 lessons, 30:48
3.Setting Up the Mongo Database4 lessons, 27:53
4.Creating an API With Express6 lessons, 29:16
5.Building the Front-End Angular App6 lessons, 45:52
6.Creating the App Components12 lessons, 1:00:02
7.Conclusion1 lesson, 03:18
3.3 Creating the Database Schema
Now that we have our connection to MongoDB up and running, the next thing that I wanna do is I wanna start to give some structure to what it is we're gonna be storing in our database. And I know what you're saying, you're saying Derrick, this is MongoDB, this is an extensible document style database, I could throw anything there I want. It doesn't have to follow the same structure. And you're right, it doesn't. But ask any developer out there that yes, even though it is flexible and you could start throwing anything in there you want, it does provide a lot of functionality and a lot of use if you do know the structure. And you predefine it at least in the beginning, and have an object or some properties with some characteristics to them that help to define what these collections look like. Even if they're not rigid structures like tables, it does definitely help to have at least a bit of a schema there, so you can follow it along. Now, yes the schema can change, absolutely, but it does help when you're defining your database in your collections to define the schema. And it just so happens that Mongoose lends itself to that quite a bit. So what we're gonna do now is we're going to create another folder, so I'm gonna say new folder, and I'm gonna call this models. Now you could call it schemas if you want, you can call it anything you want. I'm kind of a model guy, so this is what I'm going to use in this case. And I'm gonna create a new file in here, and this one is gonna be called lawn. Now, I could do this a couple of different ways, and you're gonna see why in just a minute. But in lawn.js, I'm gonna define the schema or the schemas that I'm gonna be using in this particular course. Now, there's a couple different ways you can go at this but the way that we're kinda structuring this application is that you have a lawn, or you can have any number of lawns that you're managing. And they have some characteristics, some properties, that we'll define. And then we're also gonna be tracking the applications of whatever it is you're gonna be putting on that lawn, so it could be fertilizer, could be weed killer. It could be any sort of application of some sort of product that you're gonna put on that lawn. So let's just start with that and we'll see where this takes us. So the first thing that I have to do is I have to bring in Mongoose like I've done before. So I'm gonna say const mongoose and I'm gonna require, Mongoose just like that. And so now in the terminology of Mongoose we're gonna start to create some schemas, and I'm gonna create two schemas. I'm gonna create one for the lawn, and I'm gonna create one for the applications of things we're putting on the lawn. So let's start with lawn. So I'm going to start with the lawn, and I'm gonna say const lawn. And since we're creating a schema I'm gonna call this the lawn schema, and I'm gonna set that equal to mongoose.schema. Now what this takes is an object that contains the properties that you want to add into this schema as well as some other things that you can associate with them. So you can specify a lot of things, you can specify the type that's gonna be stored in there, is it a string, is it a number? I can specify whether or not it's required, I can specify some default values, and a lot of things like that. Now there's some really good documentation on the MongoDB website about schemas, and if you wanna learn more about what you can put in here, as far as characteristics of these schemas, then I would highly recommend you go there and take a look. So we're gonna create some basic structure here. We're gonna have a total of four properties that we're gonna assign to the lawn. So we're gonna start with three and then we'll add the fourth one in, and you'll see why in just a moment. So the first thing that we're gonna call this is, let's just say we have a title. We can specify a title for this particular lawn. So you could call it home or house or apartment or whatever, you can call it whatever you want, and we're gonna specify some properties here. So this is gonna be a type, we can specify the type, this is gonna be a type of string, and we can specify whether this is going to be required or not. So is it going to be required? Yes, we're going to specify that as true, let's go ahead and specify an address. You might wanna know where this lawn is, so you can track it properly. We're gonna give this a type of string as well, this isn't required, you don't have to put it in if you don't want to. You can just give it a title and maybe your title is descriptive enough, then you can know what it is, so it's not a big deal. Size is a good thing to know when you're talking about lawns, you need to know the square footage of how much space you have that's covered in grass. So you can properly calculate how much fertilizer you need to put on it or things like that. So the type of this is going to be a number, and I think required, in this case, is beneficial, so we're gonna say required is true. Now, we're gonna add one more in here, but we need to specify the other schema first. So the next thing that I wanna put in here is going to be a collection of all the applications that I have applied to this lawn. Now in order for me to do that, I need to specify what these applications look like. So I'm gonna create another schema where this is gonna be the application schema, and we're gonna set this equal to mongoose.schema, just like we did previously. And it's gonna take in another object and we're gonna have small properties here. So let's specify a name, so we need to specify the name of this application. So is this fertilizer, is this weed killer? What is it so I can know how much of what I have put in there. So I can say that type is gonna be a string. And this is gonna be required, I think a lot of the things in the application is gonna be required, cuz we're gonna need to know what we put down and when. So the when is what we're gonna do next. We're gonna do a date here. So, the type is gonna be a date, the requiredness of this is going to be true because we're gonna wanna know when this actually happened, and also even specify a default here. We can specify a default value and we'll just say date.now is going to be the default, and then finally we'll wanna know the amount. How many pounds of fertilizer did you put on the lawn? And knowing this is important if you're tracking how much fertilizer, how much nitrogen, how much of phosphorus? And all this kind of stuff, how much of all of this have you put into your lawn? So I'm gonna specify a type here and we're gonna call this a number again, and is this required? Yes this is going to be required, I think it's gonna be very important for you to know how much you have put into your lawn. So now that I've defined this schema, I can also create connections between these schemas. So in the lawn here, I can also specify a child object, so in this case, I want to have applications, is gonna be another property. And in this case I can specify that this is going to be an array of application schema, just like that. So, as you can see I can create these connections and these linkages between these schemas, simply by using one type in the definition of another, so that's pretty nice. So now that I've come that far, now I need to make these accessible outside of this. And the way that I do that is by once again doing a module.exports like we've done so many times before, and what are we exporting? We're gonna export mongoose.model, so we're gonna export a model called Lawn, and we are gonna build that model from our LawnSchema. Now, obviously, depending on how you wanna do this, you could create multiple files and split LawnSchema and ApplicationSchema into multiple files. You can track them as multiple collections within your database, and however you really wanna structure this is completely up to you, but in my case I think this is gonna be just fine. So this is the basic application and LawnSchema structure that we are gonna make available throughout entire application as a model. But in the next lesson, I'm gonna show you one other cool thing that we can do with Mongoose and MongoDB. And that's kind of creating a little bit of a data access layer so that we can kind of having a common mechanism all throughout our application, to get into Mongoose, and ultimately into our database.