How to connect Python Flask with MongoDB?

A lot of data is sent and received everyday. These data may be name, age, email, images, documents and many more. But where is this data stored?

Huge data is stored in the cloud securely, so that it can be accessed quickly when necessary. You can even create your own application and store data of your customers in the cloud.

Wanna know how to do it? Follow this article till last in order to create your application.

Python Flask and MongoDB

Flask is a lightweight python framework which is used to create and design amazing web apps. Web apps are those which you can access online on browsers. Python Flask provides a wide range of sessions, and facilities in order to make your first web app. Now once you create a Python Flask app, say you want to store the user name and email in the cloud. Is there any way to do it? Sure there is.

MongoDB is a NoSQL database which provides cloud services. You can connect your Flask application with MongoDB and store the user details in the cloud. MongoDB even supports free cloud services which provide Shared RAM and 512MB storage. Of course you have an even better option, only if you upgrade to the premium plans of MongoDB to enjoy dedicated servers.

With this in mind, let’s create a database in MongoDB?

Step-by-step guide to Create a MongoDB Cluster

This Step-by-step tutorial will help you create a connection between MongoDB and Python.

Step1: Create your MongoDB account

First of all to explore MongoDB you need to get inside it, by creating your MongoDB account, it’s free.

Step2: Build your First Cluster

Once your account is created, Build your first cluster.

Step3: Choose Cloud Provider and Region

We need a free service, thus choose the Free shared host and select cloud service provider and region. This free service provides shared RAM with 512 MB storage. Now scroll below and rename the Cluster 0 and give it a name. Once you name the cluster you can’t undo it.

MongoDB provides AWS, Google Cloud, and Azure cloud services.

Step4: Connect Database

Now that your Cluster is created, you need to connect it to a database. You can click on Connect from the Atlas console window.

Step5: Setup Connection Security

You need to know a specific IP address, you can choose Access from anywhere or enter your own IP address. Then comes the crucial part. Enter a username and password for your database. This is required to connect Python and MongoDB.

Note: Do remember the password of the database, you will need it later. It can either be the same as the login password or different.

Step6: Choose a connection method

Select the Connect your Application and choose Python and its version from the driver drop-down. Now mark the full driver example code and copy it. This client code will then be used in the Python flask.

Database is now connected.

Step7: Browse collection

Now that you have the database connected, you now need to configure the collection. Collection is a kind of sub division of Database, which is similar to table in SQL.

Step8: Database and Collection name

Once you click Browse Collection, add your own data and enter database name and collection name.

Now connect MongoDB database with Python Flask

First of all, good going, you have successfully created a cluster and database. Now before proceeding further install the pymongo module. You can install this third party python module with help of pip.

pip install pymongo

Now you have pymongo installed and database connected, lets perform Pymongo CRUD operations in the Flask application. CRUD means create, read, insert and delete, these are the basic operations performed on the data.

Perform MongoDB CRUD operations using Flask

All things considered, let’s get into the coding part. First import the modules i.e., Flask and Pymongo. Second, paste the MongoDB client full driver code and assign database and collection. Here is the minimal Flask app syntax:

What else do we require?

Since this is a web app, you will require a static file that includes CSS, JS files. You will even need to create HTML templates to render Flask applications. Create a Form to receive user details.

As you are now familiar with Flask Syntax, paste the full driver Python code. Replace <password> to your-password. Configure the database name and collection name. There are two syntax to initialise database and collection.

  • Database
  1. db=client.database_name
  2. db=client[‘database_name’]
  • Collection
  1. clt= db[“collection_name”]
  2. clt= db.collection_name

Finally lets perform MongoDB CRUD operations.

MongoDB CRUD

First C stands for Create or Insert. This is used to add the new information into the database. You can do this by using insert attributes i.e., clt.insert_one() or clt.insert_many().

Second R stands for Read, to retrieve the data. You can perform this by find() operation.

Third U stands for Update. This is used to change the existing data and update it using update() function, which uses $set. $set outputs documents that contain all existing fields from the input documents and newly added fields.

Last but not the least D stands for Delete. To remove the data from the database record. You can do this by delete_one or delete_many collections.

You can learn about MongoDB CRUD in depth here.

Conclusion

Since this was the basic tutorial article a simple HTML form was used. You can add styling to form and update the code as you feel convenient. Explore more about Pymongo and perform CRUD operations in depth.

You can Learn MongoDB online.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.