The applications nowadays are generating lots of data and that data generally has no specific schema. To fulfill this requirement, we need to move to the database that has the capability of horizontal scaling and that database should be schema-less. The databases that fall under this category are referred to as NoSQL databases. MongoDB, CouchDB, Cassandra, HBase, Redis are some of the examples of NoSQL databases. Out of which, in this article, we will be seeing how to use the MongoDB database. But the database alone cannot solve the real use cases. So, we will be building a simple Flask application and perform the CURD operations by integrating Flask and MongoDB together.
So, let's start…
- Python3 and Flask should be installed
- MongoDB account should be created.
Step 1: Create MongoDB cluster
Login to MongoDB and go to Atlas. Click on Build a cluster.
Under shared clusters, click on create a cluster.
Click on create cluster button.
Wait till the cluster is being created.
MongoDB will create three replicas of the database for the high availability of the data and provide fault tolerance.
Step 2: Create a database user for MongoDB
Click on Database Access from the options in the left pane. Now, click on Add New Database User button.
I’m will be selecting the authentication method as a Password. Enter the username and I will generate a password by clicking the Autogenerate Secure Password button. This will generate a secure password.
Click on the copy button to copy the autogenerated password and store that password at some secure location. This passowrd is very important and if you lose it, you will not be able access this database later.
Click on Add User button. This will add a new user, in my case, I have named this user admin.
Step 3: Get a URL for connecting to the MongoDB cluster
Click on the Clusters option from the left pane.
Now, in the cluster sandbox, click on CONNECT button.
This will open up one pop-up window. Select Connect your application option.
Select Python as a driver and appropriate version of python.
Copy the connection URL and replace <password> with the password of the admin user that we have created in the previous step.
Now, we have the MongoDB database ready to use.
Let’s create a Flask application for using this MongoDB database.
Step 4: Creating a Flask application
For understanding the basics of the Flask, refer to Step 1 from this article.
For integrating MongoDB with Python, we required a MongoDB driver. In this article, we will be using pymongo as a MongoDB driver.
Install pymongo using the following command
$ pip install pymongo
Also, we are using MongoDB URL to connect to the MongoDB database is in mongodb+srv format. So, we required the dnspython package to be installed for using this URL.
$ pip install dnspython
First of all, create one file named mongopass.py and define one variable for storing MongoDB connection URL.
Don’t forget to replace <password> with your actual MongoDB password.
Now, create a file named app.py. This file will be the entrypoint of this Flask application.
Here, we will establish the connection to MongoDB with the help of the MongoClient method. The mongopass variable is the variable that contains the MongoDB connection URL with a password.
After that, we will create a database named curd and inside this database, we will create a collection named myColl. The reference to the collection is stored in the myCollection variable and we will use this variable further for doing all of the operations on the data inside the collection.
After that, we will create a / route. This will be the default route when the user opens the website. This route will render a home.html page. So, let's create a home.html page.
Here, you may think that after uploading your code to GitHub your database password will be visible. Let me give you a solution to solve this issue.
Here, we have created a separate file named mongopass.py and stoted the URL with database password here. To avoid commiting MongoDB password to GitHub, create a .gitignore file and add the path of mongopass.py file with respect to the project to this .gitignore file.
Step 5: Create web pages for the Flask app
By default, Flask detects the HTML files present inside the /templates folder as templates. So, we will create all of the HTML files inside /templates folder.
Lets create home.html file.
When we click the Start Application button, it will take us to the /curd route.
The /curd route will display a webpage that will allow us to do CURD(Create, Update, Read and Delete) operations on the MongoDB database.
First of all, let's create the webpage for /curd route and after that, we will create the route in app.py file.
After doing the operations, we have to display the result or messages. So, for this, we will create one page named response.html.
Step 6: Create routes for the CURD operations
Finally, let’s create routes for the CURD operations. We will create routes named /read, /insert, /delete, /update.
After creating all of the routes, the app.py file will look like this-
Step 7: Run the application and test the functionalities implemented
Before running the application let’s check once if the database and the collection is empty.
Let’s run the Flask app. Run the following command to start the Flask web app.
$ flask run
Go to http://127.0.0.1:5000/
Testing Insert function:
Let’s go to database collection and check if data added.
Data is added.
Testing Read function:
Click on the Read Data button to read the data.
Testing Update function:
Let’s update the address of the already added record.
Let’s check if data is updated or not.
Data is updated.
Testing Delete function:
Enter the name whose record you want to delete and click submit.
Lets check if record is deleted.
So, finally, we have implemented basic database functionalities by integrating Flask and MongoDB. Using the same approach, you can implement other advanced use cases as you required.