Defining schemas for collections in Mongoose

show more Defining schemas for collections in Mongoose provides you with in-depth training on Developer. Taught by Joseph LeBlanc as part of the Node.js Essential Training (2013) show less
please wait ...

Defining schemas for collections in Mongoose

One of the features of MongoDB is that your documents do not have to follow any sort of predefined structure. While this allows for a lot of flexibility, you usually want your data to conform to a predefined structure. In this video, we'll create a schema for arriving flight data as a way of defining that structure. First, let's go to the exercise files and get our project. Go to chapter seven, video two and then copy the start folder to the desktop. Then go to Terminal, change directory to the start folder, I'm just going to drag it in to get the path, and then type npm install.

Now let's open this code in a text editor. I'm going to start by creating a new folder. This folder is going to be called schemas. Next, I'm going to add a schema. The name of this file is going to be flight.js, and I'm going to store it in the schemas folder. Now I'm going to start building out a schema for flight data. First, I'm going to require mongoose. Now one thing to note here is that we're pulling in mongoose directly. We're not pulling in a live database connection. We're just going to use the model method of the mongoose module.

This file is going to export one thing and that's going to be a mongoose model. So type in module exports and set it to mongoose.model. Model is a method that takes two arguments The first argument is the name of the collection that we want to use. In this case I'm going to call the collection Flight. The second argument is an object. This object is where we're going to define the schema. I'm going to enter in all the fields that I want in the documents in the Flight collection.

The object property names match up to the field names. While the values determine the field types. The field types should be JavaScript natives. Now I have all the fields here that I want to store in the documents in the flight collection. There's more that you can do with a schema, such as adding your own custom logic for saves and reads. But, we'll leave it at this for the moment. Now let's go to our routes file. Whenever a flight is marked as arrived, I want to store that information in Mongo. So to do that, let's include the schema that we just defined.

So now that we have the flight schema, we can use it to save records to the database. Now let's go back down to that arrived function. Just after I call trigger arrive, I want to save that data to the database. I want that time stamp that's generated by the trigger arrive method. I'm going to create a new record based off that flight schema object. To get it set properly, we need to get the information out of the flight object. Finally, we want to save the record. So call

This method takes a function, and that function takes an argument for any errors that occur. So if there's an error, we want to log it to the console. And then we want to tell the browser that there was an internal server error, and that for some reason, we couldn't save the record. If there was no error, we just want to give the browser a success code. Now let's start up the server, and see this in action. I'm pulling up postman, and now I'm going to mark some flights as arrived.

So I've marked 33 as arrived, let's mark another one. Let's mark 18. So that one's also marked as arrived. Lets also read one of these flights. I'm going to read flight 33, and it's showing the actual arrive time here. Mongoose makes it possible to define a schema for data and then use that schema to save it. Data types are enforced with JavaScript native types. In the next video, we'll query the data we just saved to the database.

Defining schemas for collections in Mongoose
Video duration: 3m 47s 3h 17m Intermediate


Defining schemas for collections in Mongoose provides you with in-depth training on Developer. Taught by Joseph LeBlanc as part of the Node.js Essential Training (2013)

JavaScript Node.js
please wait ...