This course has been updated! We now recommend you take the API Design in Node.js, v4 course.
Transcript from the "MongoDB Introduction" Lesson
>> Scott Moss: Cool, let's hop right into the next thing.
>> Scott Moss: MongoDB and data persistence, who here has used MongoDB? Dang, okay, well, let's just skip this one and go to the next, okay, I'm sorry. If everybody used Mongo then this one shouldn't be so bad. I guess the learnings you might receive here is like, how to best use it with Express and where to use it.
[00:00:23] And maybe any opinions that I might have on it. But if anybody here has used it, people don't mind if you use it, then you should get a little bit from this. So MongoDB is a No-SQL document store to store our application's data, what does that mean? It's just a database that doesn't have, it's a non relational database that is not SQL, basically.
[00:01:00] And it is schemaless, MongoDB is schemaless. Now, Mongoose, the ODM or the ORM document, the object document model or the object relational map. Or whatever you want to call it, is the go-to ODM for MongoDB. There's some other ones out there, but Mongoose is the champion. If you go to the MongoDB websites, they'll tell you to use Mongoose, everywhere is basically saying use Mongoose.
[00:01:23] And Mongoose is actually pretty good, it's inspired other ODMs, ORMs to adopt their query language. It's one of the most flexible query ODMs or ORMs that I've seen pretty much for a while. It does provide schemas for saving data. Even though Mongo doesn't have schemas, when you use Mongoose, you do have schemas, so that's really cool.
[00:01:46] It has things like validations, things you would expect from schemas. This needs to be required, this has a min length, this must be this type, it has those validations built in, which is really cool. The Querying API is an abstraction over the MongoDB Querying API, so it's even better.
[00:02:01] It's promise based, you can do some pretty advanced stuff. And then you can also drill down and do native Mongo driver things as well. Lifecycle hooks, so like on save, on update, after update, pre save, all that stuff, you can do different things, it's really cool. And then, one of the best features I think is the run time join tables, which are populations, anybody know what populations are?
[00:02:25] Okay, so who here has used a relational database? All right, so you know what join tables are, right? You have a join table, right, using relations, Mongo doesn't have join tables. Mongoose doesn't have join tables, but they emulate it, they do something called populations. So basically, let's say I have a person and I want to associate this person with some friends, which are other people, right?
[00:02:48] You wouldn't make a join table for that like you would in a relational database. Instead, what you would do is you would just store a reference to it with an ID. Very similar to a relational database, you'd have your key ID or whatever. You would store a reference and say, hey, this user has a list of friends and here are their user IDs.
[00:03:04] It's just an array of user IDs, right? And then when you query that user, and you want to get that user's friends, by default, you usually get back an array of IDs. You're like, cool, cool story, what do I do with this? And I've got to loop through this array and get all the friends, right?
[00:03:16] Well, populate, what it does is it does that for you at runtime. Sort of like, cool, I'm going to take this array of user IDs, and for each one, I'm going to go populate it with an actual user. So it's going to join it at runtime for you, so it's like a runtime emulated join table.
[00:03:32] It's not saved that way, it's given to you that way. It's like a virtual join table, if you will, so it's really cool, very powerful. You can join per property, sub properties, I think it's really flexible and yeah, without it, Mongo and Mongoose would be pretty garbage.