Check out a free preview of the full API Design in Node.js (using Express & Mongo) course:
The "Exercise 7" Lesson is part of the full, API Design in Node.js (using Express & Mongo) course featured in this preview video. Here's what you'd learn in this lesson:

In this exercise, you will connect to a Mongo database. Once the database connection code is in place, you can verify the connection through the router.

Get Unlimited Access Now

Transcript from the "Exercise 7" Lesson

[00:00:00]
>> [MUSIC]

[00:00:03]
>> Speaker 1: So we have a small exercise. If we checkout to step 7. It's another small one.
>> Speaker 1: Let me open it up.
>> Speaker 1: There we go okay, so in step 7 there's a new file in the root, this is called ddtester. And with this file, what we're going to do.

[00:00:39] Hold on, let me silence my phone.
>> Speaker 1: There we go. This file [COUGH] is completely outside of the application we just been working on all morning. It's a separate file, that's why I made it on the root. It's just a way for us to play with mongoose and Mongo and get familiar with it.

[00:00:57] All you need to do in here is just need to connect to a database so the below will work. So once you connect to a database, [COUGH] the stuff that's happening out here, we'll be able to work and we can verify it. So what's happening below is that we're creating a new schema and making a to do's collection with a to do model.

[00:01:16]
>> Speaker 1: So they're saying the collection in the model. The model is like the representation of the actual resource that we're going to be creating or consuming in our API. The collection is where that model is stored in the database. So we're making a to-dos collection with a to do model.

[00:01:37] It's like if you use backbone, it's the same stuff. We then create a new todo.
>> Speaker 1: So once you connect to the database, if you go back in the Mongo shell, or terminal, or robo Mongo, wherever you're looking at Mongo. Switch over to your database and query it, query it there to see if you find the todo that's created below.

[00:02:03]
>> Speaker 1: And also make sure that Mongod is running, so Mongo d is running inside of the terminal, otherwise none of this will work. So make sure you have Mongo running, connect to the database up here, and then go look at it wherever, whether that's in Robomongo or whether that's in the Mongo shell, connect to that same database and your preferred method.

[00:02:23] And make sure that when you query the to dos collection, that's the name of the collection, pluralize, lower case, that you see this todo that we created.