As many of you might know that MongoDB is a highly popular non-structured database used by many growing organizations such as Google, Adobe , eBay etc. MongoDB Atlas could be used to store your applications data in the cloud. Even though it is highly scalable and easy to manage, one of the main downsides of it is that, only 512mb of storage would be available to use for free and any extension of storage should be purchased.
Mongoose is used to structure the data that is stored in the database. Through Mongoose you can set constraints to the data, perform crud operations and also write queries which is a little similar to the way SQL databases work. This defines how data should be arranged and this structure is also known as the DB schema.
Configuration of MongoDB Atlas and Mongoose
Express backend framework will be used to demonstrate how both MongoDB Atlas and Mongoose are interconnected and also basic functionalities and installation will be explained.
This is how the folder structure of the backend would look like after all the configuration.
FoodModel.js : This is where the schema is defined
FoodRoutes.js : This is where the CRUD operations are defined
server.js : This is the file that uses express to connect to the cloud database
Step 1 : Installing Express module to your project
Step 2 : Setting up MongoDB Atlas
First you need to create an account and create a cluster to work on. You can create a free cluster of 512mb of storage for free and if you require more storage you will have to make payments accordingly. You can select the desired cloud provider and finally create your cluster.
Once you create your cluster you will be able to see it your dashboard
Click connect to connect your cluster with your application
Then you need to click connect your application and add the user details and also add your ip address and you will be able to see the url to connect your application
Step 3 : Connecting MongoDB Atlas cluster with your application
This file should import express, mongoose and also the routes files. The mongoose.connect() function takes in two parameters in the example. The first is the string you copied from the cluster and the second is the useNewUrlParse which is basically used to prevent an error from showing up. You need to make sure to replace the <password> with the password provided in the MongoDB atlas. Then this will set a server on port number 3000.
Step 4 : Define the Schema
Like mentioned before the data is structured in this file. The constraints to certain attributes are defined here such as data type , whether it should contain a value or not etc. These constraints are called options. This also needs to import the mongoose module. The modules should be imported wherever it is used.
Step 5 : Defining CRUD operations in routes
There are many operations in Mongoose but we’re only going to look at 4 main functions. They are find(), save(), findByIdAndDelete() and findByIdAndUpdate().
i . Reading data from the collection | find()
This method returns the whole collection of the food model and send it as response.
ii. Creating a new instance in the collection | save()
This creates a new instance in the collection based on the inputs from the request object and sends the food object as a response
iii. Deleting a specific instance from the collection | findByIdAndDelete()
Each instance is identified by some attributes uniquely and by passing this id through the response object it could delete that particular instance by getting that ID through the request object.
iv. Updating a specific instance from the collection | findByIdAndUpdate()
This receives the id the same way as the delete function and updates the data of that specific instance which are received through the request object. The save() method is used afterwards to save the update on that instance.