Getting started with Flask-PyMongo

Flask is a micro web framework written in Python. MongoDB is a cross-platform document-oriented NoSQL database. Lets see how to connect our Flask app with MongoDB Atlas with Flask-PyMongo Python library.

Step 1 — Create an Account on MongoDB Atlas Website

Go to https://www.mongodb.com/cloud/atlas and click Try Free green button on the top right corner to create an account.

Step 2 — Create a Cluster

Create a new cluster. MongoDB allows us to create one cluster for free. Then click on collections and create a database with a collection in that database

Step 3— Get Connection String

Click Clusters →connect, a popup window will show up:

From this window, you can add your IP address to the configuration so that your computer can access the data base. Or you can add IP address 0.0.0.0/0 to the configuration so that any IP address can access the data base. You will also need to create a MongoDB user by typing your user name and password.

Then click choose a connection method on the bottom right corner. Choose connect to your application on the new window, then you will see a window like this:

Choose Python as Driver and choose the right version of the Python you are using and you will see the connection string.

Step 4— Create Flask App

Create a Python environment and install required python libraries with following commands:

Then create app.py file and add PyMongo to your code and replace <password> and <dbname> in your connection string with your password and database name:

We can test our app by executing app.py and go to http://127.0.0.1:5000/test . If it worked, then we can proceed further.

It is also possible to create multiple PyMongo instances, to connect to multiple databases:

CRUD OPERATIONS

In this article, we have created and used ‘users’ collection. We should configure our app to work in ‘users’ collection:

1— Create Operations

We can create/insert a user by:

We can also create/insert multiple users by:

2— Read Operations

We can read/select all the users by:

We can also read/select particular users by using a filter as follows:

We can also read/select only one user by:

3 — Update Operations

We can update a particular user by using filter as follows:

We can also update multiple users by:

We can also update a user if exist else create/insert the user by using upsert=True as follows:

4 — Delete Operations

We can delete a particular user by using filter as follows:

We can also delete multiple users as follows:

SAVING AND RETRIEVING FILES IN MONGODB

1— Saving a file

We can upload a file and store it in our collection by:

We can test this by executing app.py and go to http://127.0.0.1:5000 . This will execute index function. Index function will return a form. Upload any file with any name in given text-box field and click submit button. This in turn will execute save-file function and saves the file with given name in your collection.

2— Retrieving a file

We can able to retrieve a file from our collection by:

Execute app.py and go to http://127.0.0.1:5000/<name> . Replace <name> by the name you given while uploading a file to your collection.

Source code:

You can check out the code for the above app in the following Github Repository:

Congrats! You have successfully connected your Flask app to MongoDB Atlas and learned all the CRUD operations.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store