Okay, so now we got a connection to MongoDB or Mlab and now what we want to do is we want to create
a model and save this model to the MongoDB database.
So let me show you really quick what is a model and how do you make one?
Um, so let's go ahead and make a new file and call it the Reddit article.
And in here we will import mongoose again.
So mongoose.
And then we want to make a variable here called Reddit article.
And we want to say mongoose dot model.
And then we put in the name like a string of the model name.
So Reddit article again, and then we put in a schema in here.
So, um.
So mongoose.
Schema.
And then we pass in what sort of what is the data structure of this model look like?
So we have a title that's a string.
And.
Now I just press save and it's formatting it a bit and then we can export the model.
So rigid article module exports Reddit article.
And that's a really simple model we can use inside of MongoDB and save it.
So now let's go inside of our Index.js.
And in here we need to import the model we just made.
So read it article.
Require.
Read it article like that.
I'm just going to delete this commented out string here because we're not using it.
So we have the model here Reddit article and now we just need to save it or actually create it.
So we go const Reddit article.
And we say new Reddit article, and then we pass in some options, which is going to be the values of
this model.
And now since we're using JavaScript, ES6, since our variable here, we're trying to assign has the
same name as the property here, you can actually just write title here.
I just type this out to make it more explicit for you guys who are not used to this feature, But if
you're using JavaScript ES6, you can just do like this because the property and the.
And the object we are trying to assign to it is the same name anyway.
Once we're done that we created the model.
Now we can just go read it article.
That save and maybe put a weight in front and, um, that's basically it.
Now let's try and see inside of our M-lab database.
Okay, so we got the connection here with Reddit articles.
It has zero documents.
Now, if I try and.
And run this code here.
See connected to M-lab.
And then all the Reddit articles comes out.
Now, if we refresh this one, the side here, we should be seeing a lot of documents having been created
inside of the collection here.
So you see, 26 documents has been created inside of the collection.
And if you go and click the connection, we can also see all the documents.
So document is basically the same as the model you have created in here.
A Reddit article.
And there you go.
You see all of the different Reddit articles being created in here.
And yeah, that's pretty much it.
Now.
A lot of mongoose tutorials will show a different way of calling save, which is not using async await,
but I like to use async await.
I think it makes the code a lot of cleaner, so if you can help it try and use async await.
I would definitely recommend that.
Um.
Yeah.
So that's it.
That's how you make a really simple mongoose model, save it, connect to a mongoose database or MongoDB
database.