A Streamlit‑based graphical interface for MongoDB that supports full CRUD operations, database and collection management, index and schema validation, and 12 predefined aggregation pipelines (including a join pipeline).
The backend uses PyMongo and the provided MongoDbOperation class; outputs are beautified with the Rich library.
- Features
- Prerequisites
- Setting Up MongoDB Atlas
- Configuration & Installation
- Running the Application
- Using the Interface
- Predefined Pipelines
- Troubleshooting
- License
- Database management – list, create, drop databases
- Collection management – list, create (with JSON Schema validator), drop, show validation rules
- Full CRUD – insert (one / many), fetch all, update (one / many), delete (one / many)
- Index management – list indexes, create ascending unique index, drop index
- Schema validation – modify validation rules of an existing collection
- 12 predefined aggregation pipelines – executed on the
Test.carsorstore_db.users/orderscollections (see below) - Sample data seeding – populate the required collections with the provided car, user and order data
- Beautiful output – JSON results are rendered with syntax highlighting; Rich library improves readability in the terminal and in the Streamlit UI
- Python 3.8 or higher
- A MongoDB Atlas account (free tier works perfectly)
- Basic knowledge of MongoDB (collections, documents, aggregation pipelines)
Follow these steps to create a MongoDB Atlas cluster and obtain your connection string.
- Log in to MongoDB Atlas.
- Click Create (or Build a Database).
- Choose the FREE (M0) tier.
- Select a cloud provider (AWS, GCP, or Azure) and a region close to you.
- Give your cluster a name (e.g.,
MyCluster) and click Create Cluster. - Wait a few minutes for the cluster to be ready.
- In the left sidebar, go to Database Access.
- Click Add New Database User.
- Choose Password authentication.
- Enter a username and a strong password.
Important: If your password contains special characters like#,@,/, you must URL‑encode them later when building the connection string. The provided code usesquote_plusto handle this. - Set Built‑in Role to
Read and write to any database(orAtlas Adminfor full control). - Click Add User.
- In the left sidebar, go to Network Access.
- Click Add IP Address.
- For development, you can click Allow Access from Anywhere (
0.0.0.0/0).
(For production, restrict to your specific IP.) - Click Confirm.
- In the left sidebar, go to Database.
- Click Connect for your cluster.
- Choose Connect your application.
- Select Python and the latest version.
- Copy the connection string. It looks like:
mongodb+srv://<username>:<password>@<cluster-url>/?retryWrites=true&w=majority
- Replace
<username>and<password>with the credentials you created.
If your password contains special characters, URL‑encode them (the code does this automatically).
Place the following files in the same directory:
pymongo_config– theMongoDbOperationclass (provided)pymongo_pipelines.py– Aggregation pipelines and sample dataapp.py– the Streamlit interface (code provided in the answer)
Create a virtual environment (optional but recommended) and install the required packages:
pip install streamlit pymongo richThe pymongo_config.py file currently contains hard‑coded credentials (username your_username and a password).
- Open
pymongo_config.pyand locate the__connectmethod:
username: str = "your_username"
password: str = quote_plus("your_password")
uri: str = f"mongodb+srv://{username}:{password}@mycluster.ewovdrg.mongodb.net/?retryWrites=true&w=majority&appName=MyCluster"If your password contains special characters, quote_plus will encode them automatically – do not pre‑encode them yourself.
- Optional – Use environment variables (more secure):
import os
username = os.getenv("MONGO_USER", "your_username")
password = quote_plus(os.getenv("MONGO_PASS", "your_password"))- Then set MONGO_USER and MONGO_PASS in your shell before running Streamlit.
- From the terminal, inside the directory containing
app.py, run:
streamlit run app.py- Streamlit will open a new tab in your default browser. If it doesn’t, you can manually open
http://localhost:8501.
- The left sidebar contains a radio menu with seven categories. Select any category to expand its controls.
-
List all databases– shows every database in your Atlas cluster. -
Create database– creates a new database (by inserting a dummy collection and removing it). -
Drop database– permanently deletes a database.
-
List collections– shows all collections inside a given database. -
Create collection– creates a collection; you can optionally provide a JSON Schema validator (e.g., {"$jsonSchema": {"required": ["name"]}}). -
Drop collection– deletes a collection. -
Get collection info– displays the validation rules (if any) for a collection
-
Insert document(s)– accepts a JSON object (single document) or a JSON array (multiple documents). -
Fetch all documents– retrieves every document from the selected collection and displays them as JSON. -
Update documents– specify a filter (JSON) and the update values (JSON). Choose one or many. -
Delete documents– specify a filter and whether to delete one or many documents.
-
Show indexes– lists all indexes on a collection. -
Create index– creates an ascending unique index on a single field (field name is required). -
Drop index– deletes an index by its name.
Modify collection schema– updates the JSON Schema validator of an existing collection. The new validator must be a valid JSON Schema object.
-
Select one of the 12 predefined pipelines from the dropdown.
-
Click Execute selected pipeline.
-
The pipeline runs on the correct
database/collection(most onTest.cars, the join pipeline onstore_db.users). -
The JSON result is displayed with syntax highlighting.
-
Before using the aggregation pipelines, you need to insert the sample data:
-
Insert cars into
Test.cars– loads 14 car documents. -
Insert users into
store_db.users– loads 5 user documents. -
Insert orders into
store_db.orders– loads 5 order documents. -
These buttons will create the databases and collections automatically if they don’t exist.
| # | Name | Description | Target Collection |
|---|---|---|---|
| 1 | Group by fuel_type & count engines >1000cc | Groups cars by fuel type; counts total and those with engine >1000cc | Test.cars |
| 2 | Add is_diesel flag | Projects model and a boolean whether fuel_type contains "Dies" | Test.cars |
| 3 | Average price per model | Computes average price for each model | Test.cars |
| 4 | Hyundai cars uppercase names | Converts maker+model to uppercase, writes to hyundai_cars collection | Test.cars |
| 5 | Add 55,000 to price | Adds a constant to the price field | Test.cars |
| 6 | Price in lakhs (string) | Converts price to a string like "12.5 lakhs" | Test.cars |
| 7 | Total service cost per Hyundai car | Sums the service_history.cost for each Hyundai car | Test.cars |
| 8 | Categorise fuel as Petrol_car / Non_petrol_car | Adds a new field based on fuel_type | Test.cars |
| 9 | Budget category based on price | Adds budget_cat: Budget (<5L), Mid_range (5L-10L), Premium (>10L) | Test.cars |
| 10 | Service cost status (High/Low) for Hyundai | Adds cost_status based on total service cost (≥10000 → High) | Test.cars |
| Name | Description | Target Collection |
|---|---|---|
| Users with orders | Performs a $lookup from users to orders on user_id | store_db.users |
- All operations use MongoDB Aggregation Framework
- Ensure proper indexing for better performance
-
Verify your IP is whitelisted in MongoDB Atlas Network Access.
-
Check that the username and password in
pymongo_config.pyare correct. -
If your password contains special characters, make sure you use quote_plus (the code already does).
- Use the Create database / Create collection buttons first, or use the Seed Sample Data buttons which create them automatically.
-
Ensure the target collection contains data (run the seed buttons).
-
Some pipelines (e.g.,
pipeline_4) write results to a new collection – check thehyundai_carscollection.
- The app falls back to plain text if Rich rendering fails. Make sure you have installed rich (
pip install rich).
- This project is provided for educational purposes. You are free to modify and use it as needed.
This README covers everything: what the app does, how to set up MongoDB Atlas, how to configure credentials, installation, running, and detailed usage. The user can simply copy‑paste this into a `README.md` file in the project root.