This course has been updated! We now recommend you take the API Design in Node.js, v4 course.
Transcript from the "Using the Mongo with Node" Lesson
>> 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.
>> 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.
>> 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.
>> 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.