Adding Mongoose in MEAN Stack
In our previous section, we set up our MongoDB on cloud storage. Now, we have a database, and we need a way to connect to that database from within our node express application. So, in this section, we will learn about the way through which we can connect to our database.
We will use a third-party package, i.e., MongoDB client package and actually, we can use two packages here, i.e., mongodb and mongoose. We can install the mongodb package using npm in the following way:
But, we will use the mongoose package. Mongoose is a third-party package that builds upon the official MongoDB driver, but it makes accessing MongoDB much easier and more convenient. Mongoose use schemas that mongodb does not really use, so we can define how our data should look like and that allows us to conveniently store and fetch data.
This might not be the solution we are looking for if we have the requirement of unstructured data. But often, that's not the case, and therefore, Mongoose is a great tool that could even work with such unstructured data.
We will install the mongoose package by using the following npm command:
After downloading this into our project folder, this tool will allow us to connect to our MongoDB and also to interact with it to store data and fetch data.
Now, we will use the Mongoose. We will use the following steps to use it:
1) On our backend, firstly, we need to add the schema for that. We will go to our model.ts file, and this model shows us how a post looks like right now. We don't need to create a unique id on our own because the ID will be created automatically by Mongoose for us. Instead of ID, we need to focus on creating these schemas on the backend. For this, we will create new folder models.
3) In this file, we will import the Mongoose using the 'require' method in the following way:
4) To create the schema, we first need to create a blueprint for how our data should look. We will use the Schema method of the mongoose package to create a blueprint in the following way:
In the above code, we define the field and type of data in the form of an object. We defined the title, which is of type string and add the metadata, i.e., required and default. We can learn more about this in mongoose official docs.
Therefore, we will get an error if we try to create this without having a title property set.
6) We will also add another field, i.e., content in the same way as we have created for the title. After defining this, our schema will be defined.
7) It is just a blueprint, and in order to create data or create models objects, we need to turn the definition into a model. We will do that by using the mongoose model function. The model function takes two arguments, i.e., the first argument is the name of the model, and another one is our schema, which we want to use. We will use the model function in the following way:
8) Now, we need to export this because we will use this mongoose model outside of this model file. We will export this model using the exports syntax in the following way:
In the next section, we will use this mongoose model in our app.js file. We will use it when we send a post request to API posts.