Check out a free preview of the full Introduction to MongoDB course

The "Connecting to the Database" Lesson is part of the full, Introduction to MongoDB course featured in this preview video. Here's what you'd learn in this lesson:

Scott introduces the process for requiring Mongoose, what port to explicitly bind to, how to connect locally, and how to manage warnings in the terminal stemming from mismatched Mongoose and MongoDB versions.

Preview
Close

Transcript from the "Connecting to the Database" Lesson

[00:00:00]
>> Scott Moss: So knowing that, the first thing that we need to do is we need to connect to our database.
>> Scott Moss: So the way you do that is actually pretty simple with Mongoose. So I'm just gonna make a new file here called test. And basically once you install Mongoose which is already installed for you, you just need to run mpm install, you're already there.

[00:00:21]
We're just gonna require it.
>> Scott Moss: And it's pretty simple to connect. The only thing you have to do is, I'm just gonna make a function or connect and you'll see why in a minute. Is you can use the Mongoose.connect function or function or method here. And the way that you connect the database as Mongoose is like pretty straight forward, you just have to give it a URL.

[00:00:49]
And I'm just gonna give you the format of what Mongoose URL looks like. The first part is gonna be the protocol, the protocol for mongodb is gonna be mongodb, that's gonna be the protocol. Just like in URL in the browser, the protocol's HTTP, or a web socket is ws.

[00:01:06]
For Mongo, it's mongodb, so that's the protocol that you're gonna use for URLs. Once you've got the protocol, you're gonna type in the host name to it. In this case, because you're on your machine, the host name is gonna be localhost. So we're gonna say localhost. So we've got the protocol, we've got the host name, and then you need to add the explicit port.

[00:01:27]
By default, everyone here should most likely be binding to the same default port that Mongo binds to, which is gonna be 27017. That's gonna be the default port that Mongo binds to. You can change that if you want, but I would just keep it there to save. Cuz if someone else uses this, and they didn't set that setting, this is gonna be annoying.

[00:01:46]
So just keep like that there. Unless, for whatever reason, you just love that port and you got something else running on it, then yeah. So once you have that, the next thing is the name of the database. And you can put whatever you want here. Remember, if it's there, it'll connect to it.

[00:02:00]
If it's not there, it'll create it. So you can put literally whatever you want here, it doesn't matter. And now you're connected to the database. And mongoose.connect return to promise. So you can just put a return here. And now we have a connect function that returns a promise that connects to the database.

[00:02:18]
And that's about it. There's this other option here in Mongoose that has like different types of options. And depending on what version of Mongoose you might be running. All of you are running the latest version. You might have the passes in different parameters. You might get some warnings in the console that says, hey you have to pass this option.

[00:02:32]
Just listen to those warnings and pass those options. And like I said it is going to depend on what versions of Mongoose and combination of Mongo that you have. They change it all the time. Sometimes Mongo updates, but Mongoose might be lagging behind so you'll see a lot of deprecated warnings in the terminal.

[00:02:50]
Sometimes you can't do anything about that, cuz it's not your code, it's Mongoose internally. So just try your best to adhere to those warnings, but if it's like a deprecation warning that you can't deal with, then don't worry about it.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now