How to Connect Mongodb Compass to Flask
Last Updated :
28 Apr, 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.
Python3
from flask import Flask
from pymongo import MongoClient
# Flask constructor takes the name of
# current module (__name__) as argument.
app = Flask(__name__)
Connect Flask to MongoDB database
Here, we are connecting to a MongoDB server running on the local machine on the default port 27017.
Python3
client = MongoClient('mongodb://localhost:27017/')
Accessing Specific Database
We are accessing the specific MongoDB database.
Python3
db = client['eshop-database']
collection = db['users']
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.
Python3
documents = collection.find()
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.
Python3
@app.route('/')
# ‘/’ URL is bound with hello_world() function.
def hello_world():
documents = collection.find()
for record in documents:
print(record)
return "HEllo"
# main driver function
if __name__ == '__main__':
# run() method of Flask class runs the application
# on the local development server.
app.run()
Complete Code:
Python3
# Importing flask module in the project is mandatory
# An object of Flask class is our WSGI application.
from flask import Flask
from pymongo import MongoClient
# Flask constructor takes the name of
# current module (__name__) as argument.
app = Flask(__name__)
client = MongoClient(
'mongodb://localhost:27017/')
# The route() function of the Flask class is a decorator,
# which tells the application which URL should call
# the associated function.
db = client['eshop-database']
collection = db['users']
@app.route('/')
# ‘/’ URL is bound with hello_world() function.
def hello_world():
documents = collection.find()
for record in documents:
print(record)
return "HEllo"
# main driver function
if __name__ == '__main__':
# run() method of Flask class runs the application
# on the local development server.
app.run()
Output:Â
Terminal output
URL
Similar Reads
How to Connect Node to a MongoDB Database ? Connecting Node.js to MongoDB is a common task for backend developers working with NoSQL databases. MongoDB is a powerful, flexible, and scalable database that stores data in a JSON-like format. In this step-by-step guide, we'll walk through the entire process from setting up your development enviro
6 min read
How to Connect to MongoDB Atlas Using Shell? MongoDB is a highly scalable NoSQL database, renowned for its ability to manage vast amounts of complex and unstructured data. Unlike traditional databases that use a tabular format, MongoDB employs a document-oriented approach, storing data in a flexible, JSON-like format. This makes MongoDB highly
5 min read
How to Install MongoDB Compass on Linux? MongoDB is a free, open-source, cross-platform, document-oriented database. It is also classified as a non-SQL database program. Compass is an interactive tool for querying, optimizing & analyzing MongoDB data. It works in a visual environment. It can be installed in any operating system like Wi
1 min read
How to Connect to a MongoDB Database Using Node.js MongoDB is a NoSQL database used to store large amounts of data without any traditional relational database table. To connect to a MongoDB database using NodeJS we use the MongoDB library "mongoose". Steps to Connect to a MongoDB Database Using NodeJSStep 1: Create a NodeJS App: First create a NodeJ
4 min read
How To Connect MongoDB Database in a Node.js Applications ? To connect MongoDB to a Node.js application, you can follow a simple process using the Mongoose library, which provides a flexible way to work with MongoDB. Mongoose acts as an Object Data Modeling (ODM) library, making it easier to structure and interact with MongoDB from Node.js.Prerequisites:Node
2 min read