VOOZH about

URL: https://www.geeksforgeeks.org/python/how-to-connect-mongodb-compass-to-flask/

⇱ How to Connect Mongodb Compass to Flask - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Connect Mongodb Compass to Flask

Last Updated : 23 Jul, 2025

An effective GUI tool for managing and visualizing MongoDB data is MongoDB Compass. On the other hand, a well-liked Python web framework for creating web apps is called Flask. Integrating your MongoDB data into your Flask web application may benefit from connecting MongoDB Compass to Flask. Through your Flask web application, you can quickly read, write, and alter data saved in MongoDB thanks to this integration.

Required Modules

pip install pymongo

Connect MongoDB Compass to Flask

Import the PyMongo module

At first, we are importing the Flask into our code and we are creating a Flask app.

Connect Flask to MongoDB database

Here, we are connecting to a MongoDB server running on the local machine on the default port 27017.

Accessing Specific Database

We are accessing the specific MongoDB database.

Retrieving Data from Specific Database

In this step, we are using a cursor that will return a cursor object that you can iterate over to access the individual documents.

Running the Flask App

In this step, we are creating a route that prints the data from MongoDB on running the Flask app and hitting the URL.

Complete Code:

Comment
Article Tags: