Check out a free preview of the full API Design in Node.js (using Express & Mongo) course:
The "Using the Mongo with Node" 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:

Mongoose is a JavaScript diver for Mongo that will expose an easier-to-use API inside a Node environment. Mongoose will also add support for APIs like Promises and allow the data to establish relationships and schemas.

Get Unlimited Access Now

Transcript from the "Using the Mongo with Node" Lesson

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

[00:00:04]
>> Speaker 1: So this is great when we use the Mongo shell in the terminal to do stuff, but obviously we won't be using the Mongo shell in our API. That will not be fun, right? So we need some type of JavaScript driver to access mongo. There is a native driver that we can use that will work fine, if you just npm install a mongo JS you get the native driver.

[00:00:24] And it pretty much allows you to do the same stuff we just did in this shell, in our JavaScript files. Which, like I said, it’s pretty easy, but it gets even easier. And there's really not a lot of options here. What we'll be using is ORM, or I guess in a NoSQL context, an ODM.

[00:00:45]
>> Speaker 1: Everybody know what an ORM is? Does anybody not know what an ORM? So ORM is just, you can think of it as just a library that helps us talk to the database.
>> Speaker 1: That's all it is, it's a library in some language that will allow you to talk to the database with your preferred programming language.

[00:01:13]
>> Speaker 1: It's short for object relational management?
>> Speaker 2: Mapper.
>> Speaker 1: Mapper. There we go, yeah.
>> Speaker 1: So Mongoose is the most popular one for Mongo, and it's the preferred one. It's actually, I think, the official one now. It's like the thing. So Mongoose is the one that we'll be using.

[00:01:34] It is not made by the people who made mongo. It's an open, third-party ORM. It's actually made by, who’s it made by? Automatic. The people who own WordPress. So, they're the ones who made it. And it's super awesome. It has a really, really good API, one of the best APIs I've seen on any REM in JavaScript.

[00:01:58]
>> Speaker 1: So Mongoose will abstract things away.
>> Speaker 1: It also adds supports for things like promises. Allows us to model our data with schemas, cuz remember I said, I have so many typos. Remember I said, up here, mongo is a NoSQL database, we don't need to model our data, we can just throw stuff in it.

[00:02:17] So we don't really need schemas with mongo, as you saw, I just threw an object in there. I can just put anything that I want in here. Any properties, if I wanted to put an array of here, nums,
>> Speaker 1: That's fine. It'll put it in there. It doesn't matter, right?

[00:02:39] It doesn't care. Mongoose adds those features so we can care about stuff like that. So it'll allow us to develop these schemas just like you would have inside a relational database, and allow us to establish relationships with our models and so much more. So if you ever use a relational database you have different types of relations, right?

[00:03:03] You have one to one relationships, one to many relationships, many to many relationships, right, inside a relational database, which will determine how you store these references on different tables. Or even join tables in different situations, like many to many and stuff like that. So we're able to do that with Mongoose.

[00:03:23] But just note that mongo doesn't care, as we just saw. Is everybody following me there? Okay.
>> Speaker 1: So here's an example of how you connect to mongo, using Mongoose. And it's just this simple. You require(‘mongoose’), after you NPM install it, and then you call mongoose.connect, and then you have to use this mongodb protocol, whereas typically on the URL you have htp or whatever.

[00:03:58] Well when you connect to our database it uses a different protocol, and that's the mongodb protocol. So, we put mongodb there, ://localhost. You don't need a port, it doesn't matter if you can see through the port. And then the next is the name of the database that you want to connect to or create.

[00:04:15] So, just like when we said use whatever, if it existed it'll switch to it, if it doesn't exist it'll just make it. It's the same thing that's happening here. If you give it a name to database if it exists it'll connect to it, if it doesn't exist it'll create it.

[00:04:33]
>> Speaker 1: And that's how you connect to a database, that's it. So now with that, we're now connected to the database and we're able to start doing queries and doing all types of stuff.
>> Speaker 1: Any questions on that?
>> Speaker 1: So mongoose.connect, that's it.