- Creating a Free MongoDB Cluster
- Configuring Database Access
- Setting Up Network Access
- Setting Up Flask
- Running the Flask Application
- Bonus & Notes
- Go to MongoDB Atlas and create a new cluster.
- Choose the free plan, provide a unique name for the cluster.
- Select a cloud provider (AWS, Azure, or GCP) and choose a region (e.g., Mumbai (ap-south-1)).
- Click Create to finalize the setup.
- Create a new database and provide a unique name.
- Add a collection name (must be unique as well).
- Add a user for authentication.
- If you haven't created a user, create one now.
- Navigate to Security > Database Access.
- Click Add New Database User.
- Choose an authentication method (password-based authentication recommended).
- Enter a username and password (save them for later use).
- Assign a role with Read & Write Access.
- You can add a temporary database user for limited-time access (6 hours - 1 week).
- Go to Security > Network Access > IP Access List.
- Click Add IP Address.
- Add your current IP address.
- You can add temporary IP addresses for limited-time access.
- For learning purposes, you can set 0.0.0.0/0 (Allows access from anywhere).
- A faster way is using Security > Quickstart, where you’ll find all necessary options.
- Install dependencies using:
pip install -r requirements.txt
- Alternatively, create a virtual environment and install dependencies:
python -m venv flask-app-env flask-app-env\Scripts\activate # Windows pip install -r requirements.txt
Create a .env file and add the following details:
MONGO_URI= "Enter your MongoDB Connection String"
MONGO_DB_NAME= "Enter Database Name"
MONGO_COLLECTION_NAME= "Enter Collection Name"
SECRET_KEY= "Enter your Secret Key (Random Alpha-Numeric)"
- SECRET_KEY is used for session management, encryption, and CSRF protection.
- Rename sample.env to .env before running the application.
Run the application using one of the following commands:
flask run
OR
python app.py
This guide ensures a smooth integration of Flask with MongoDB for your application. 🚀 You can contribute to this demo app, submit PRs 👍