Introduction to MongoDB Schemas & Why Mongoose
Transcript from the "Schemas & Why Mongoose" Lesson
>> Scott Moss: Let's get started. So, does everybody have the repo?
>> Scott Moss: Yep, okay. So if you have the repo, basically what we're gonna do is we're gonna start off doing a couple of things. But when we first started the course I said yes, you can use Mongo without schemas.
[00:00:17] But I highly recommend not using Mongo without schemas. It's just, I just don't recommend doing it. I've even talked to the people who work at Mongo and it's like yeah, just don't use Mongo without schemas. In fact, the latest MongoDB driver for Node just started supporting schemas and validations.
[00:00:34] So even the first party ORM is supporting schemas now. So yeah, you definitely wanna use schemas. But it doesn't mean it's a relational database because you have schemas. It's just a way to validate your data, and make sure that the shape is consistent. So you're not duct taping on the front end, or your mobile app, trying to figure out what's there and what's not, cuz that's just horrible.
[00:00:55] So you kinda need schemas. So what we're gonna do is, we're gonna walk through a few exercises on a couple of things. Because the exercise that you're about to do next, is you're gonna be developing some schemas with some validations, and developing some credit queries. So I'm gonna show you a little bit of things to get started doing that, because it's actually quite simple.
[00:01:12] So I'm just gonna open up this code editor here. So the first thing we need to figure out is, how do we connect to a Mongo database from a Node.js application? That's the first thing. Without that, you can't really do anything. So the way you do that is, you basically have two options.
[00:01:28] You can use the native MongoDB driver, which is pretty cool. It's getting better. Or you can use a package called Mongoose. Who here has heard of Mongoose before? You've heard of Mongoose? Mongoose, yeah? Mongoose is awesome. I use Mongoose for everything. I don't see a need to even use the native Mongo driver, because Mongoose just kinda does the job.
[00:01:48] It actually just passes straight through to the MongoDB driver for a lot of other things that it does, so you kinda get the best of both worlds. Mongoose is more modern, and they just have features that the MongoDB driver just hasn't supported yet, although they are catching up.
[00:02:01] And it's got really good support, so that's what we're gonna be using. We're gonna be using Mongoose for our ORM, or ODM, which stands for Object Rational Mapper, or Object Relational Mapper, or Object Data Mapper. However you wanna call it, it's one of those things. It's the same thing like SQL.
[00:02:19] Sequelize for postgres, or Bookshelf for postgres, any one of those ORMs. Waterline if you've ever used that. Nex, all those ORMs for SQL. This is the ORM for Mongo, and it's called Mongoose. And it's definitely the de facto one. And it's probably one of the biggest reasons why I use MongoDB, because Mongoose is so good.
[00:02:41] Without Mongoose I don't know if I would even use MongoDB, that's how good it is. I've tried to use DynamoDB before, and I love DynamoDB, but I'm so spoiled by Mongoose I can't even use DynamoDB. I'm just like, it's just not even close.