How to Connect Flask Application with MongoDB on CentOS
Connecting MongoDB with a Python Flask Application
Prerequisites
Before starting, ensure you have the following:
- Flask installed in your Python environment.
- MongoDB installed locally or a running MongoDB server.
- Python 3 installed with
pip
. - Basic knowledge of Flask and Python.
Step 1: Install Required Python Packages
Install the necessary Python packages to connect Flask to MongoDB:
pip install Flask pymongo dnspython
pymongo
is the official MongoDB driver for Python, and dnspython
helps with DNS resolution for MongoDB URIs.
Step 2: Set Up a MongoDB Database
Start MongoDB: Ensure your MongoDB server is running. If you installed MongoDB locally, use the following command to start it:
mongod
Create a Database: Use MongoDB Compass or the CLI to create a new database, for example,
mydatabase
.Create a Collection: Inside
mydatabase
, create a collection, for example,users
.
Step 3: Configure Flask Application
Create a file named app.py
and include the following code to connect your Flask application to MongoDB:
from flask import Flask, jsonify, request
from pymongo import MongoClient
app = Flask(__name__)
# Replace with your MongoDB URI
MONGO_URI = "mongodb://127.0.0.1:27017"
client = MongoClient(MONGO_URI)
db = client['mydatabase']
users_collection = db['users']
@app.route('/add_user', methods=['POST'])
def add_user():
user_data = request.json
result = users_collection.insert_one(user_data)
return jsonify({"message": "User added", "user_id": str(result.inserted_id)})
@app.route('/get_user/<name>', methods=['GET'])
def get_user(name):
user = users_collection.find_one({"name": name})
if user:
user["_id"] = str(user["_id"])
return jsonify(user)
return jsonify({"error": "User not found"}), 404
if __name__ == '__main__':
app.run(debug=True)
Step 4: Run the Application
Run your Flask application using the following command:
python app.py
Testing Endpoints
Add a User: Use a tool like Postman or
curl
to test the/add_user
endpoint:curl -X POST -H "Content-Type: application/json" \ -d '{"name": "John Doe", "email": "[email protected]"}' \ http://127.0.0.1:5000/add_user
Get a User: Test the
/get_user/<name>
endpoint:curl http://127.0.0.1:5000/get_user/John%20Doe
Step 5: Use Environment Variables for Configuration
Avoid hardcoding sensitive information. Use environment variables for the MongoDB URI.
Using python-dotenv
Install python-dotenv
:
pip install python-dotenv
Create a .env
file:
MONGO_URI=mongodb://127.0.0.1:27017
Update app.py
:
from dotenv import load_dotenv
import os
load_dotenv()
MONGO_URI = os.getenv("MONGO_URI")
client = MongoClient(MONGO_URI)
Step 6: Advanced Usage
- Indexes: Create indexes in your collections to improve query performance.
- Authentication: Use a MongoDB URI with username and password for authenticated databases.
- Connection Pooling: Configure connection pooling in
MongoClient
for better performance.
Conclusion
By following these steps, you can connect a MongoDB database to your Flask application. This setup is compatible with a variety of Flask extensions and forms the foundation for building scalable web applications.