Javatpoint Logo
Javatpoint Logo

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:


Adding Mongoose in MEAN Stack

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.

Adding Mongoose in MEAN Stack

2) In the models folder, we will create a new JavaScript file and give it a name post. In this file, we will create our post model using Mongoose.

Adding Mongoose in MEAN Stack

3) In this file, we will import the Mongoose using the 'require' method in the following way:


Adding Mongoose in MEAN Stack

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:


Adding Mongoose in MEAN Stack

5) In the schema, we will pass a javascript object. This object will hold our custom configuration. We define the fields and the type of data that we want to store in these fields. We will define all these things 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.

Adding Mongoose in MEAN Stack

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.


Adding Mongoose in MEAN Stack

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:


Adding Mongoose in MEAN Stack

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:


Adding Mongoose in MEAN Stack

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.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA