WEBVTT

00:03.730 --> 00:06.550
Hey did everyone that they share and welcome to another video.

00:06.550 --> 00:09.270
And in this video, again, we are going to talk about three.

00:09.280 --> 00:10.810
I know you love to write code.

00:10.810 --> 00:12.070
I also love to write code.

00:12.070 --> 00:16.630
In fact, in majority of my courses, I like to just go through into the code later and write that.

00:16.630 --> 00:18.040
But this is interesting theory.

00:18.040 --> 00:19.120
This is important theory.

00:19.120 --> 00:22.360
You should know about it, otherwise you'll be mindlessly writing the code.

00:22.360 --> 00:23.290
We don't want that.

00:23.290 --> 00:27.040
We want you to write code efficiently by full understanding of it.

00:27.220 --> 00:32.740
So let's talk about MongoDB in depth and what Mongoose role does at play and why this business logic

00:32.740 --> 00:35.050
comes into the picture even at the first place.

00:35.170 --> 00:40.630
So imagine this is your MongoDB and this is where you want to store all of your data persistently.

00:40.630 --> 00:42.550
So this is how it looks like.

00:42.550 --> 00:48.220
Assume that your MongoDB usually it gives a kind of abstraction when you install it onto your same computer,

00:48.220 --> 00:49.630
but it's not like that.

00:49.630 --> 00:54.760
This whole box denotes that MongoDB is installed onto some another computer, some another machine,

00:54.760 --> 00:57.370
and that's where this MongoDB is now.

00:57.370 --> 01:03.490
The picture where MongoDB can come in, you can directly talk to it or you can inject a mongoose.

01:03.670 --> 01:10.090
The mongoose is again an ODM, which helps you to interact with MongoDB with easier methods and some

01:10.090 --> 01:14.350
of the precooked method that are already there in Mongoose, which you are going to study in this video

01:14.350 --> 01:17.440
and I'll show you some of the clean examples that Mongoose gives you.

01:17.470 --> 01:18.880
Can you work without Mongoose?

01:18.910 --> 01:19.630
Absolutely.

01:19.630 --> 01:23.650
But you have to write all of your validation code and all the checks that are being given to you.

01:23.680 --> 01:26.740
Mongoose helps you to write all these checks right out of the box.

01:26.740 --> 01:29.080
And that's thus it saves so much of the time.

01:29.080 --> 01:35.200
With us now, another question that usually I get is let's say this is the web and it is sending all

01:35.200 --> 01:39.280
the data to us, like in the request body or the forms that we have on the front end are probably in

01:39.280 --> 01:39.910
the react.

01:39.910 --> 01:45.240
So we can directly use Mongoose or maybe we can directly throw that into the database itself.

01:45.370 --> 01:48.430
No, that's where you would be absolutely horribly wrong.

01:48.430 --> 01:53.560
It's like almost you buy the food from the market and you flush it down directly to the toilet.

01:53.590 --> 01:54.910
No, you don't do that.

01:54.910 --> 02:00.070
We always go ahead by the food, we process the food, we cook it and we digest it and then it goes

02:00.070 --> 02:00.520
down.

02:00.520 --> 02:05.440
So again, make sure you understand your data needs to be processing first and then throw it up into

02:05.440 --> 02:08.800
the database without the back end or without the business logic.

02:08.800 --> 02:11.080
You cannot just dump the data itself.

02:11.080 --> 02:15.400
I know it's a weird example, but it always stuck with the student and the always remember throughout

02:15.400 --> 02:15.910
the life.

02:15.910 --> 02:21.460
So that is why whenever the data comes up, whether it's coming from React, mobile application, react,

02:21.460 --> 02:25.720
flutter, whatever is your favorite one, you go ahead and add this into the back end.

02:25.720 --> 02:28.540
And this is what we are designing so far into the picture.

02:28.540 --> 02:31.870
So once this actually comes in, then you add some of the business logic.

02:31.870 --> 02:36.040
Maybe you want to authenticate whether somebody has passed on an email, which is a correct email,

02:36.070 --> 02:40.900
correct format or not, your password is fully secured or not, or at least you are using six digits

02:40.900 --> 02:41.590
or something.

02:41.590 --> 02:43.930
So you perform all those validation first.

02:43.930 --> 02:48.880
Once those all validation are done, you have massaged your data, fixed all of the data that whatever

02:48.880 --> 02:53.410
you want to do it, and then after that you talk to the mongoose and say, Hey, Mongoose, I want to

02:53.410 --> 02:56.710
add some data into the database on top of my validation.

02:56.710 --> 02:58.510
Can you add your validations also?

02:58.510 --> 03:03.040
Can you also give me some easier method to do the current operation in the database?

03:03.040 --> 03:08.770
And that's where Mongoose helps you and actually go ahead and finally sends all the data to the MongoDB.

03:08.800 --> 03:11.980
Now, one final advice that I would like to 100% give you.

03:11.980 --> 03:17.110
Please always remember this you will never face any trouble in the database is remember that your database

03:17.110 --> 03:19.120
is always on different continent.

03:19.150 --> 03:20.230
No, it is not.

03:20.230 --> 03:22.930
In the ideal case, you want to keep it on the same continent.

03:22.960 --> 03:27.220
If you are using eight US or something, you probably be having database in Mumbai or something.

03:27.220 --> 03:32.230
But I always recommend all of my student to assume that it's on different databases, different continent

03:32.230 --> 03:32.710
itself.

03:32.710 --> 03:33.700
Why is it so?

03:33.700 --> 03:38.440
Because once you understand that your database is on a different continent, you will always remember

03:38.440 --> 03:43.690
that it takes time when you add some data into the database or retrieve some data from the database.

03:43.690 --> 03:48.730
So it always, no matter what, it goes into a sink and a weight or any kind of similar situation.

03:48.730 --> 03:51.670
Remember, it is going to take some time in your local development.

03:51.670 --> 03:57.880
It's instance, it's really fast and the world has also brought up into the world where we are not that

03:57.880 --> 03:58.420
much patience.

03:58.420 --> 04:00.160
So remember, patience is the key.

04:00.160 --> 04:05.080
Whatever you do, database is on different continent and when the data will travel, it will take some

04:05.080 --> 04:05.410
time.

04:05.410 --> 04:06.790
So always remember that.

04:06.910 --> 04:11.650
Okay, now that you understand this, let's also go into the Odiham section Mongoose.

04:11.680 --> 04:16.330
Now this is where we are going to learn about the elegant MongoDB object modeling.

04:16.330 --> 04:18.610
We will have a separate video on modeling itself.

04:18.610 --> 04:20.020
Don't you worry on that part.

04:20.020 --> 04:23.830
So let's go ahead and see read the docs that how we can actually read the docs.

04:23.860 --> 04:27.820
Now, first and foremost, I will show you how to connect the database and all of that.

04:27.820 --> 04:29.500
We'll have a detailed video on this.

04:29.500 --> 04:31.090
But right now, let's talk about that.

04:31.090 --> 04:33.130
This is a blog schema that they are giving.

04:33.130 --> 04:36.610
So let's just say you want to design a blog and this is how you can do it.

04:36.610 --> 04:41.230
So notice here, it's really, really easy in this case that you are saying, okay, it's not really

04:41.230 --> 04:41.560
big.

04:41.560 --> 04:43.300
The blog is going to have a title.

04:43.300 --> 04:47.080
And schema, by the way, is the structure of how your data is going to look like.

04:47.080 --> 04:51.640
Every application has a structure, your blog has image, title, subtitle and a content.

04:51.640 --> 04:53.410
So and who is the author of that?

04:53.410 --> 04:57.280
So everything needs to have a structure and we call that structure as a schema.

04:57.340 --> 05:00.400
So Mongoose helps you to design those schema.

05:00.400 --> 05:01.780
And you can see this is pretty basic.

05:01.880 --> 05:06.200
You'll have a title which will be of type, string author, string body comments and all of that.

05:06.200 --> 05:06.950
So pretty.

05:06.960 --> 05:09.940
Okay, there is no such extra thing which mongo is giving us.

05:09.950 --> 05:15.710
And yes, I would agree on this point that in this example we don't see the hidden power of the mongoose

05:15.710 --> 05:20.300
that gives us this power actually comes up when you scroll a little bit and look into the validation

05:20.300 --> 05:21.020
section.

05:21.020 --> 05:22.400
Now, right out of the box.

05:22.400 --> 05:25.610
Mongoose gives you so much of the validation that you can use.

05:25.610 --> 05:27.530
For example, in this case the name.

05:27.530 --> 05:29.780
This is a type string, but the required is true.

05:29.780 --> 05:34.940
So this field or this, whenever you're trying to save this into the database, Mongo Mongoose will

05:34.940 --> 05:36.950
alert you that, hey, this value is not there.

05:36.950 --> 05:38.030
This is required.

05:38.120 --> 05:43.040
Not only that, you can actually scroll it a little bit and you can see that you can actually provide

05:43.040 --> 05:49.040
the custom messages even that this X the type is number and the minimum is there and the max is there.

05:49.040 --> 05:51.440
So you have to choose between six and 12.

05:51.440 --> 05:55.910
And if you don't choose anything or if you choose anything else, then a custom message can be written

05:55.910 --> 06:00.890
directly into the model that Hey, it must be at least six eggs and you are choosing the less one.

06:00.890 --> 06:05.690
So right out of the box you get the ability to check the things right out of the box.

06:05.690 --> 06:11.180
No custom validation we have written and we can also send an error message which is stored in the central

06:11.180 --> 06:13.250
model of the entire database.

06:13.250 --> 06:14.300
So this is so good.

06:14.870 --> 06:18.590
Now we also have some of the unique capabilities to find out whether it is unique or not.

06:18.590 --> 06:21.440
And they also give you some warnings in case you want to check that out.

06:21.440 --> 06:24.320
But you can also go ahead and write your custom validators.

06:24.320 --> 06:29.600
If you know about the regex or the regular expression, you can write your custom regex and can test

06:29.600 --> 06:31.040
your values for this.

06:31.040 --> 06:32.450
What are the values coming in?

06:32.450 --> 06:34.100
And then you can return based on that.

06:34.100 --> 06:38.660
The message that, hey, this is not a valid phone number or whatever, so this is just the start of

06:38.660 --> 06:40.430
it that what mongoose actually can do.

06:40.430 --> 06:46.970
You you can also look for the queries, by the way, the queries are the simple way that there are few

06:46.970 --> 06:48.740
operations in the database, not too many.

06:48.740 --> 06:53.570
You just create new values or you just go ahead and search for them or query with that.

06:53.570 --> 06:58.370
And further down the road you can query to update, you can query to delete whatever you want to do.

06:58.370 --> 07:00.770
And these are the only methods that you have to study.

07:00.770 --> 07:02.240
And these are really, really short.

07:02.240 --> 07:07.850
You have to just delete some operations you can find by ID or some values that you can pass on or you

07:07.850 --> 07:09.470
can find and delete, find an update.

07:09.470 --> 07:14.480
So these are not too many compared to what actually you have to learn if you are learning the entirety

07:14.480 --> 07:15.260
of the database.

07:15.260 --> 07:17.630
So this helps me so much here.

07:17.660 --> 07:23.060
Now coming back up to there, you can also go ahead for connections and can do so much on the connection.

07:23.060 --> 07:28.370
There are even hooks provided that if I'm saving the data into the database before saving the data,

07:28.370 --> 07:33.530
do this operation or just after saving the data, do that operation so there can be lifecycle hooks

07:33.530 --> 07:35.000
available with the mongoose.

07:35.000 --> 07:39.380
I know this is too much of the information for you right now, and especially when I'm talking in the

07:39.380 --> 07:44.240
theory this is not making too much of sense, but eventually this is going to make sense.

07:44.240 --> 07:48.500
The reason why I'm putting this theory out right now, because when we'll be doing the actual practical,

07:48.500 --> 07:51.770
then this theory will get recalled and you will get more understanding.

07:51.770 --> 07:55.070
So this is a proven theory that I've used again and again while the teaching.

07:55.070 --> 07:56.450
So this is how we are going to do it.

07:56.540 --> 07:59.180
So again, we will be coming back on to the documentation.

07:59.180 --> 08:00.800
We'll be seeing that how we can do it.

08:00.800 --> 08:05.510
Right now, what I want to do is go ahead and explore the schema part because that's what we'll be designing

08:05.510 --> 08:07.160
that how we can define the schema.

08:07.160 --> 08:08.120
So go ahead.

08:08.120 --> 08:10.490
Creating a model, how we can do that and all of this.

08:10.490 --> 08:15.860
This will help you a little bit when we are going to actually start our project and write the code.

08:15.860 --> 08:18.920
So this is all the basics that I wanted to discuss with you.

08:18.920 --> 08:23.210
In the next video, I will talk a little bit about the data modeling and what the schema design and

08:23.210 --> 08:23.960
all of that.

08:23.960 --> 08:29.150
I will show you what kind of tools are actually used in the industry or in the production grade, and

08:29.150 --> 08:34.280
I'll show you what alternatives we can use because we don't want to buy them, at least for this course.

08:34.280 --> 08:36.200
Let's go ahead and catch up in the next video.
