WEBVTT

00:00.680 --> 00:09.230
Okay, so now we got a connection to MongoDB or Mlab and now what we want to do is we want to create

00:09.230 --> 00:13.760
a model and save this model to the MongoDB database.

00:14.060 --> 00:19.250
So let me show you really quick what is a model and how do you make one?

00:20.130 --> 00:28.850
Um, so let's go ahead and make a new file and call it the Reddit article.

00:31.380 --> 00:35.900
And in here we will import mongoose again.

00:35.910 --> 00:39.030
So mongoose.

00:39.750 --> 00:44.940
And then we want to make a variable here called Reddit article.

00:45.330 --> 00:49.140
And we want to say mongoose dot model.

00:49.140 --> 00:53.670
And then we put in the name like a string of the model name.

00:53.670 --> 01:00.210
So Reddit article again, and then we put in a schema in here.

01:00.240 --> 01:01.860
So, um.

01:03.920 --> 01:06.860
So mongoose.

01:08.530 --> 01:09.310
Schema.

01:10.580 --> 01:17.450
And then we pass in what sort of what is the data structure of this model look like?

01:17.450 --> 01:21.650
So we have a title that's a string.

01:23.460 --> 01:24.360
And.

01:25.210 --> 01:32.620
Now I just press save and it's formatting it a bit and then we can export the model.

01:34.500 --> 01:39.120
So rigid article module exports Reddit article.

01:39.120 --> 01:45.180
And that's a really simple model we can use inside of MongoDB and save it.

01:46.400 --> 01:49.850
So now let's go inside of our Index.js.

01:51.560 --> 01:56.300
And in here we need to import the model we just made.

01:56.300 --> 01:58.820
So read it article.

01:59.830 --> 02:00.670
Require.

02:02.280 --> 02:04.850
Read it article like that.

02:05.090 --> 02:10.040
I'm just going to delete this commented out string here because we're not using it.

02:10.730 --> 02:16.460
So we have the model here Reddit article and now we just need to save it or actually create it.

02:16.460 --> 02:21.680
So we go const Reddit article.

02:23.380 --> 02:32.740
And we say new Reddit article, and then we pass in some options, which is going to be the values of

02:32.740 --> 02:33.700
this model.

02:34.950 --> 02:43.620
And now since we're using JavaScript, ES6, since our variable here, we're trying to assign has the

02:43.620 --> 02:49.920
same name as the property here, you can actually just write title here.

02:50.220 --> 02:56.790
I just type this out to make it more explicit for you guys who are not used to this feature, But if

02:56.790 --> 03:03.780
you're using JavaScript ES6, you can just do like this because the property and the.

03:04.700 --> 03:09.260
And the object we are trying to assign to it is the same name anyway.

03:09.680 --> 03:12.200
Once we're done that we created the model.

03:12.200 --> 03:14.960
Now we can just go read it article.

03:16.980 --> 03:24.830
That save and maybe put a weight in front and, um, that's basically it.

03:26.000 --> 03:31.700
Now let's try and see inside of our M-lab database.

03:31.940 --> 03:37.040
Okay, so we got the connection here with Reddit articles.

03:37.040 --> 03:38.990
It has zero documents.

03:39.590 --> 03:42.350
Now, if I try and.

03:43.940 --> 03:45.980
And run this code here.

03:50.910 --> 03:52.500
See connected to M-lab.

03:53.450 --> 03:56.390
And then all the Reddit articles comes out.

03:57.370 --> 04:04.720
Now, if we refresh this one, the side here, we should be seeing a lot of documents having been created

04:04.720 --> 04:06.400
inside of the collection here.

04:07.190 --> 04:12.650
So you see, 26 documents has been created inside of the collection.

04:13.600 --> 04:17.560
And if you go and click the connection, we can also see all the documents.

04:18.890 --> 04:24.530
So document is basically the same as the model you have created in here.

04:24.560 --> 04:26.030
A Reddit article.

04:26.360 --> 04:27.410
And there you go.

04:27.410 --> 04:32.300
You see all of the different Reddit articles being created in here.

04:34.900 --> 04:39.640
And yeah, that's pretty much it.

04:39.670 --> 04:40.630
Now.

04:41.980 --> 04:50.950
A lot of mongoose tutorials will show a different way of calling save, which is not using async await,

04:50.950 --> 04:53.170
but I like to use async await.

04:53.170 --> 04:59.740
I think it makes the code a lot of cleaner, so if you can help it try and use async await.

04:59.770 --> 05:01.810
I would definitely recommend that.

05:03.100 --> 05:03.790
Um.

05:03.790 --> 05:04.360
Yeah.

05:04.360 --> 05:05.110
So that's it.

05:05.110 --> 05:11.890
That's how you make a really simple mongoose model, save it, connect to a mongoose database or MongoDB

05:11.920 --> 05:12.910
database.
