Hey did everyone that they share and welcome to another video.
And in this video, again, we are going to talk about three.
I know you love to write code.
I also love to write code.
In fact, in majority of my courses, I like to just go through into the code later and write that.
But this is interesting theory.
This is important theory.
You should know about it, otherwise you'll be mindlessly writing the code.
We don't want that.
We want you to write code efficiently by full understanding of it.
So let's talk about MongoDB in depth and what Mongoose role does at play and why this business logic
comes into the picture even at the first place.
So imagine this is your MongoDB and this is where you want to store all of your data persistently.
So this is how it looks like.
Assume that your MongoDB usually it gives a kind of abstraction when you install it onto your same computer,
but it's not like that.
This whole box denotes that MongoDB is installed onto some another computer, some another machine,
and that's where this MongoDB is now.
The picture where MongoDB can come in, you can directly talk to it or you can inject a mongoose.
The mongoose is again an ODM, which helps you to interact with MongoDB with easier methods and some
of the precooked method that are already there in Mongoose, which you are going to study in this video
and I'll show you some of the clean examples that Mongoose gives you.
Can you work without Mongoose?
Absolutely.
But you have to write all of your validation code and all the checks that are being given to you.
Mongoose helps you to write all these checks right out of the box.
And that's thus it saves so much of the time.
With us now, another question that usually I get is let's say this is the web and it is sending all
the data to us, like in the request body or the forms that we have on the front end are probably in
the react.
So we can directly use Mongoose or maybe we can directly throw that into the database itself.
No, that's where you would be absolutely horribly wrong.
It's like almost you buy the food from the market and you flush it down directly to the toilet.
No, you don't do that.
We always go ahead by the food, we process the food, we cook it and we digest it and then it goes
down.
So again, make sure you understand your data needs to be processing first and then throw it up into
the database without the back end or without the business logic.
You cannot just dump the data itself.
I know it's a weird example, but it always stuck with the student and the always remember throughout
the life.
So that is why whenever the data comes up, whether it's coming from React, mobile application, react,
flutter, whatever is your favorite one, you go ahead and add this into the back end.
And this is what we are designing so far into the picture.
So once this actually comes in, then you add some of the business logic.
Maybe you want to authenticate whether somebody has passed on an email, which is a correct email,
correct format or not, your password is fully secured or not, or at least you are using six digits
or something.
So you perform all those validation first.
Once those all validation are done, you have massaged your data, fixed all of the data that whatever
you want to do it, and then after that you talk to the mongoose and say, Hey, Mongoose, I want to
add some data into the database on top of my validation.
Can you add your validations also?
Can you also give me some easier method to do the current operation in the database?
And that's where Mongoose helps you and actually go ahead and finally sends all the data to the MongoDB.
Now, one final advice that I would like to 100% give you.
Please always remember this you will never face any trouble in the database is remember that your database
is always on different continent.
No, it is not.
In the ideal case, you want to keep it on the same continent.
If you are using eight US or something, you probably be having database in Mumbai or something.
But I always recommend all of my student to assume that it's on different databases, different continent
itself.
Why is it so?
Because once you understand that your database is on a different continent, you will always remember
that it takes time when you add some data into the database or retrieve some data from the database.
So it always, no matter what, it goes into a sink and a weight or any kind of similar situation.
Remember, it is going to take some time in your local development.
It's instance, it's really fast and the world has also brought up into the world where we are not that
much patience.
So remember, patience is the key.
Whatever you do, database is on different continent and when the data will travel, it will take some
time.
So always remember that.
Okay, now that you understand this, let's also go into the Odiham section Mongoose.
Now this is where we are going to learn about the elegant MongoDB object modeling.
We will have a separate video on modeling itself.
Don't you worry on that part.
So let's go ahead and see read the docs that how we can actually read the docs.
Now, first and foremost, I will show you how to connect the database and all of that.
We'll have a detailed video on this.
But right now, let's talk about that.
This is a blog schema that they are giving.
So let's just say you want to design a blog and this is how you can do it.
So notice here, it's really, really easy in this case that you are saying, okay, it's not really
big.
The blog is going to have a title.
And schema, by the way, is the structure of how your data is going to look like.
Every application has a structure, your blog has image, title, subtitle and a content.
So and who is the author of that?
So everything needs to have a structure and we call that structure as a schema.
So Mongoose helps you to design those schema.
And you can see this is pretty basic.
You'll have a title which will be of type, string author, string body comments and all of that.
So pretty.
Okay, there is no such extra thing which mongo is giving us.
And yes, I would agree on this point that in this example we don't see the hidden power of the mongoose
that gives us this power actually comes up when you scroll a little bit and look into the validation
section.
Now, right out of the box.
Mongoose gives you so much of the validation that you can use.
For example, in this case the name.
This is a type string, but the required is true.
So this field or this, whenever you're trying to save this into the database, Mongo Mongoose will
alert you that, hey, this value is not there.
This is required.
Not only that, you can actually scroll it a little bit and you can see that you can actually provide
the custom messages even that this X the type is number and the minimum is there and the max is there.
So you have to choose between six and 12.
And if you don't choose anything or if you choose anything else, then a custom message can be written
directly into the model that Hey, it must be at least six eggs and you are choosing the less one.
So right out of the box you get the ability to check the things right out of the box.
No custom validation we have written and we can also send an error message which is stored in the central
model of the entire database.
So this is so good.
Now we also have some of the unique capabilities to find out whether it is unique or not.
And they also give you some warnings in case you want to check that out.
But you can also go ahead and write your custom validators.
If you know about the regex or the regular expression, you can write your custom regex and can test
your values for this.
What are the values coming in?
And then you can return based on that.
The message that, hey, this is not a valid phone number or whatever, so this is just the start of
it that what mongoose actually can do.
You you can also look for the queries, by the way, the queries are the simple way that there are few
operations in the database, not too many.
You just create new values or you just go ahead and search for them or query with that.
And further down the road you can query to update, you can query to delete whatever you want to do.
And these are the only methods that you have to study.
And these are really, really short.
You have to just delete some operations you can find by ID or some values that you can pass on or you
can find and delete, find an update.
So these are not too many compared to what actually you have to learn if you are learning the entirety
of the database.
So this helps me so much here.
Now coming back up to there, you can also go ahead for connections and can do so much on the connection.
There are even hooks provided that if I'm saving the data into the database before saving the data,
do this operation or just after saving the data, do that operation so there can be lifecycle hooks
available with the mongoose.
I know this is too much of the information for you right now, and especially when I'm talking in the
theory this is not making too much of sense, but eventually this is going to make sense.
The reason why I'm putting this theory out right now, because when we'll be doing the actual practical,
then this theory will get recalled and you will get more understanding.
So this is a proven theory that I've used again and again while the teaching.
So this is how we are going to do it.
So again, we will be coming back on to the documentation.
We'll be seeing that how we can do it.
Right now, what I want to do is go ahead and explore the schema part because that's what we'll be designing
that how we can define the schema.
So go ahead.
Creating a model, how we can do that and all of this.
This will help you a little bit when we are going to actually start our project and write the code.
So this is all the basics that I wanted to discuss with you.
In the next video, I will talk a little bit about the data modeling and what the schema design and
all of that.
I will show you what kind of tools are actually used in the industry or in the production grade, and
I'll show you what alternatives we can use because we don't want to buy them, at least for this course.
Let's go ahead and catch up in the next video.