Check out a free preview of the full Introduction to MongoDB course
The "Creating Schemas & Models" 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 instructs on how to create a simple schema and model.
Transcript from the "Creating Schemas & Models" Lesson
[00:00:00]
>> Scott Moss: So once we connect to Mongo, we should start actually creating some data, doing some CRUD stuff. So the first thing you have to do is you have to create some type of shapes that we actually want to add data for. And those shapes are gonna be like collections, right?
[00:00:16]
So we talked about collections, we created one in the shell. Now we're gonna create one here using Mongoose. And again, it's pretty straightforward, because we are already using something like Mongoose. So the way you create a collection is gonna be something like this. So if I wanted to create a collection called student.
[00:00:35]
>> Scott Moss: All I would have to use is the new mongoose.schema, and that's gonna take an object. So if I say new mongoose.schema, this is telling mongoose that, hey, I'm making a new schema, and by itself a schema doesn't do anything. It's just like instructions on field names and types and validation.
[00:00:56]
It doesn't actually do anything in the database. It doesn't create a collection. It's just instructions for a collection that we have to create later. So we're gonna create that schema first. It's very much the same way if you created a schema for a table or a model in Postgres or SQL, very much the same.
[00:01:12]
So I'm just gonna keep it very minimum and say I'm gonna have a student who has a firstName, and its type is string. And what's pretty sweet here is that we can use the built in JavaScript perimeters as the typings. So, string is a global in JavaScript because that's a primitive in JavaScript.
[00:01:28]
It's a global class. So, we can choose a primitive to figure out because Mongoose is based off JavaScript and that's pretty cool. We're gonna use any libraries or use a actual string of value like string. You can just say string and it knows that you wanna use a string, so that's pretty legit, I kinda like that.
[00:01:46]
That's very easy to get started. So once we have our schema, the next thing that we need to do is convert the schema over to what we call a Mongoose model, or a Mongol model. And then that model is gonna create a collection for us. So the way you do that is also pretty simple with Mongoose.
[00:02:05]
All you have to do is use mongoose.model. You pass in the name of your collection as the first argument. And what's important here is the pattern that the community uses for this is that you use a lower case name for the collection. And don't pluralized it because Mongoose or Mongo is gonna pluralized it for you.
[00:02:26]
So, whatever I type in here is gonna be pluralized. So, if I type in student, the actual name of the collection is gonna be students, lower case. So, that's why it's just keep it singular, keep it lowercase and then don't be surprised when you go look in the database is like, it's gonna be pluralized.
[00:02:42]
It's gonna be students and it's gonna be lowercase. The reason I say, keep it like this, is because once we start doing relations and referring to things, it's gonna be a lot easier to deal with if it's constant. So just say student and then the next thing you wanna do is just pass the schema as the second argument.
[00:02:58]
And now you have a model. That's gonna return a model, so you want to go ahead and say that in a variable, and we can just call that Student, with a capital S. The convention in the community is to use capital letters for models. So Student with a capital S, that's gonna be the model.
[00:03:12]
Now I'll have a reference to a model called Student, which is gonna represent a collection. So now I can save students using this student model against the student collection with just these few lines of code.
>> Scott Moss: So again in just these few lines of code we were able to connect to a database.
[00:03:31]
We have the logic for it. We didn't actually connect yet cuz we've got to run this function. We created a schema for a student and then we registered that schema as a collection called student, and it returned as a model that we can interact with inside of our application.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops