Querying MongoDB collections
Video: Querying MongoDB collectionsOnce documents are entered into a Mongo DB collection, you can query them to display the data. Mongoose has functions for querying Mongo DB collections and handling the responses. In this video, we'll query the flights collection and display the results in a view. Lets go to the exercise files. Go to chapter seven, video three and then copy the start folder to your desktop. Next, let's install the dependencies. Change directory to that start folder and then type in npm install.
Viewers: in countries Watching now:
- Why use Node.js?
- Installing Node.js
- Understanding the event loop
- Initializing Node.js projects
- Creating modules with getters and setters
- Starting Express applications
- Testing your code
- Working with sessions and databases
- Building command-line tools
- Emitting events and attaching listeners
- Controlling readable streams
Querying MongoDB collections
Once documents are entered into a Mongo DB collection, you can query them to display the data. Mongoose has functions for querying Mongo DB collections and handling the responses. In this video, we'll query the flights collection and display the results in a view. Lets go to the exercise files. Go to chapter seven, video three and then copy the start folder to your desktop. Next, let's install the dependencies. Change directory to that start folder and then type in npm install.
Then press Return. Now let's go to the code. Open the start folder in sublime text or any other text editor you prefer. Let's go to the routes index.js file. Let's create a new route handler called arrivals. Just like all the other route handlers, it's a function that accepts the request object and the response object as the two arguments. Now from the previous video, we included the flight schema from the schemas folders. We can use this flight schema to also query the mongo database.
The FlightSchema object is a chainable object. We can build up our query with different methods and then execute it. So let's do that now. Start by typing in FlightSchema and then call the find method. We could leave this query just as it is. This would pull in all the documents from the Flights collection regardless of the ordering. One thing though, we actually want the ordering to sort by the Actual Arrive field. So to do that, let's call the Set Options method. The Set Options method takes an object as the argument.
This argument allows us to set the different options for the query. So the one that we want to set is Sort. And in this case, we want to sort by the field Actual Arrive. Now finally, let's execute this query. We can do that by calling the exec function. The exec function takes a function as an argument. And then, this function is provided with an error and the record set as two arguments. I'm just going to call the results, arrivals. First, we want to make sure that there were no errors when we did this query. So first, check the error object.
If there's an error, we want to log it to the console, and then we also want it to tell the browser that there was an error. However, if there was no error, we want to render the results. The first argument is the name of the jade view that we want to render. We're going to be adding a new jade view called arrivals, so let's add that here. And then the second argument is the data that we want to pass to that view. We want to set the title of the page, so set title to arrivals. And then finally, we also want to set the arrivals themselves. Now, let's create the jade view for these arrivals.
Go to the Views folder and create a new view. I'm going to name this arrivals.jade. Let's review list.jade. On line one, there's extends layout and that's going to pull in layout.jade. Then on line three, there's block content. This is defining the content that's going to appear in layout.jade where we have block content defined. So let's build a rivals.jade in a similar way. Lists.jade is displaying flight information in a loop.
And we're going to do almost the exact same thing on the arrivals page. So, let's copy this code and paste it into arrivals.jade. Now let's begin making some adjustments so that we'd see the arrivals information. First, the name of the variable we're passing in as arrivals. Instead of dealing with our standard flight objects, we're dealing with data that's come back from mongo DB. So, we don't have to call get information. However, we do have dates that are in a very long numerical time stamp. Those aren't very user friendly, so let's change those.
I'm going to create a variable here named landed. And I'm going to set that variable to a new date object. This new date object is going to take the actual arrive property and use that to create a formatted date. And now let's replace destination with landed. There's one last piece we need to add before we start the server. And that's the route. Go to app.js and then scroll down. Let's create a new route called arrivals. And then we'll use the arrivals function, that we just defined in index.js of routes.
Now let's start the server, go to terminal and type node server. Then press return. Now lets load up the arrivals route. Go to logo host at port 3000 slash arrivals. Currently we have flights 33 and 18 marked as arrived. Lets find some other flights that we can mark as arrived as well. Go to data index dot js. Lets mark flight 71 as also arrived. I'm going to go to postman, and I'm now going to mark flight 71 as arrived.
And when I do that and hit refresh, it appears in the list. Also, we can stop and restart the server, and this data will still be there. Press Control > C to stop the server. Then press up on the keyboard to bring up node server, and then press return to start the server again. Now go back to the browser and hit refresh. All three records are still there. After creating a schema with Mongoose, you can chain calls to it to query a collection. Once the results come back from Mongo DB, you can then render them in a jade view, or use them however you wish.
The data stays in Mongo DB regardless of how many times you restart your server. In the next video, we'll use Mongo DB to restore sessions.
Find answers to the most frequently asked questions about Node.js Essential Training .
Here are the FAQs that matched your search "" :
- Q: How can I install the new version of the Express command-line tool?
- A: Newer versions of the Express command-line tool can be installed using the command "npm install -g express-generator." But before doing this, you should first remove any older version of the module, using the command "npm uninstall -g express."
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.