In the previous video, I already showed you how to connect with the MongoDB, and in this video,
let's create the some schema.
Or we can go and create a document collection inside the MongoDB database.
Now let's show you how we can do that.
Firstly, we will define the folder called schema and we will create a schema like this.
In this example you will cat.schema.
.ts
So I will show you how we can do similar like that in the post.
Over here I will create a new folder called schemas.
Sorry schemas inside schemas for I will create a post.schema.ts
Okay that the first one
And in order to create the schema, because we use the ORM or we can call ORD
right
So for that reason we will write the object object relational mapping.
We will mapping from a class into the real database.
And for that reason we will create a class to representation of the document.
So firstly, we will define a class and use the @Schema.
To make it will be the schema of the database.
And we use the prop property right here.
So this will be the column, right.
And now instead of
try to understand anything right here.
I will copy
paste and see what is the result.
Okay, let me copy everything, and paste it and firstly I will replace.
Firstly, I will
I will change the name.
Okay.
From the cat schema and cat document into the post document.
Okay, Post.
Document.
Post
Here and now.
You don't need to worry about what is inside here.
What is HydratedDocument ?
Okay.
You don't need to
You don't need to know.
All the that, this is not important.
The important is make the code working.
This will be good with the post.
I need, I need the title.
And with the that title, we need the string.
Right?
And the description will be string also.
And I will remove everything right here.
Okay.
That's for schema.
And now let me open the terminal a little bit right here.
And I will scroll down a little bit.
We can see, with prop.
We can mark this with data type like this.
Or we can use the validation I will show you how to validation later.
Don't worry that for now we can reference this with different.
Document different collection.
Now we scroll down a little bit.
Right here.
The code right here is what we use inside the Express.js framework, right?
With the Expressjs Framework, we use something like this.
But with NestJS, we will prefer the class approach.
Way
And now, let's go to the module and import the MongooseModule.forFeature and define something like
the name of the database and the schema.
Let me show you how we can do that. Inside the post module right here.
I will copy okay and never remember the code over here.
I will copy until the array right here and inside the import.
We don't have import here, right?
Import.
And instead of import from the Cat I will use Post.
Right.
Oh, sorry.
The Post is not from "nestjs/common"
The Post comes from the schema.
The schema.
And I will import the PostSchema.
Now.
And make sure you a comma.
It's a little bit.
And the post right here.
It will be the class, right?
And the schema here is in the schema.
We already export right here.
And because this schema right here belongs to the post module.
So we will import this inside the post module.
And that's it, let me go to the MongoDB.
And click inside the cluster.
And click on the browse collection.
Inside the collection right here.
Now, you can see we have a database and a posts right here.
Right.
The database name's default will be "test" because we don't name for it.
If I go to the app.module, I will name for it, for example like post_db
Okay.
And I will drop the test, type "test" to the drop all of that
And if I close the project and run it again let's see what happened.
Okay.
Now we already have a post_db and the posts collection, right?
Very cool
And that for the video.
And let me summarize.
In this video, we already understand how we can create a collection and some documentation.
We will work with documentation later.
Okay, we use the schema and prop to define the property and schema for defined collection, and we
will export that.
But you don't need to worry about what is the document or the schema factory or .createForClass
We don't need to know all that.
Okay, this is not important.
In later we can use the post document right here for example like the data type.
But I will work with that later.
Don't worry for now.
So that's it for the video.
I will see you in the next one.