VOOZH about

URL: https://www.geeksforgeeks.org/python/sending-data-from-a-flask-app-to-mongodb-database/

⇱ Sending Data from a Flask app to MongoDB Database - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Sending Data from a Flask app to MongoDB Database

Last Updated : 4 Jun, 2026

Flask provides a lightweight framework for building web applications and APIs, while MongoDB offers a flexible NoSQL database for storing data. The project covers setting up a Flask application, connecting it to MongoDB using PyMongo, creating a POST endpoint, and inserting JSON data into a database collection.

Prerequisites

  • Knowledge of Python and a machine with Python installed.
  • Understanding of basic concepts of Flask.
  • MongoDB is installed on your local machine if not you can refer to this article.

Configuring MongoDB

Open MongoDB Compass and connect to the local MongoDB server. After a successful connection, the database instance will be ready for use in the Flask application.

👁 Image
 

Setup a Development Environment

Configure the virtual environment for development, this step can be skipped but it is always recommended to use a dedicated development environment for each project to avoid dependency clash, this can be achieved using a Python virtual environment.

# Create gfg folder
$ mkdir gfg

# Move to gfg folder
$ cd gfg

👁 Image
 

A folder named gfg is created to store the project files, although any folder name can be used. Navigate to the project directory using the cd command and create a virtual environment by running the following command:

$ python -m venv venv

Now to use the virtual environment we need to first activate it, this can be done by executing the activated binary file.

$ .\venv\Scripts\activate # for Windows OS
$ source venv/bin/activate # for Linux OS

👁 Image
 

Installing Dependencies for the Project

With the development environment set up, install the required dependencies for the project. Flask is used to build the web application, while pymongo enables Python applications to interact with MongoDB databases.

$ pip install Flask pymongo

👁 Image
 

Next, connect a FLask app to MongoDB and send some data into it.

Creating a Flask App

After setting up MongoDB and installing the required libraries, create a Flask application and connect it to the MongoDB database to store user data.

Create a `main.py` file in the project directory and add the following code to the file.

Over here a starter Flask App is created with a single route that returns a string "Hello, World!", Now test this thing out by running the app as shown below:

Output:

👁 Image

The app is up and running now let us test the output at `http://127.0.0.1:5000`,

👁 Image

Connecting Flask App to Database

We have got a running starter Flask App with some basic code let's connect it to the MongoDB database using the following script.

The above script will connect to the MongoDB database that we configured earlier now we need a post route to add some data in the database which can be done as follow:

Here a route named `/add_data` is created which when invoked with a post request reads the JSON data from the body and inserts it into the database.

For reference here is the overall code that I used for the demo.

Example JSON Payload

{
"name": "John",
"email": "john@example.com",
"city": "New York"
}

Sending Data from Flask to MongoDB

Now test the entire script and if we can insert some data into the database using it, First run the Flask App shown before then make a POST request to `/add_data` a route using a tool like Postman.

👁 Image

The response above looks fine let us check the MongoDB database if there is any data inserted or not.

👁 Image

As you can see a database named demo is created with a collection of `data` with a single document that we just inserted using Flask.

Related Articles

Comment
Article Tags: