Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
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.
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 record.save.
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.
There are currently no FAQs about Node.js Essential Training.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.