A simple Flask REST API with MongoDB for basic CRUD operations on users.
- Docker and Docker Compose installed on your machine.
- If you have a local MongoDB instance running, stop it to avoid port conflicts.
sudo systemctl stop mongod
# or alternatively
ps aux | grep mongod
Clone the repository:
git clone https://github.com/yourusername/flask-mongo-crud.git
cd flask-mongo-crud
Bring down any running containers (if any) => Build and start the Flask app with MongoDB:
docker-compose down
docker-compose up --build
Your API will be accessible at: http://localhost:5000 (or http://172.21.0.3:5000 if using a specific Docker network)
| Method | Endpoint | Description |
| ------ | ------------- | ----------------------- |
| GET | `/users` | List all users |
| GET | `/users/<id>` | Get a user by ID |
| POST | `/users` | Create a new user |
| PUT | `/users/<id>` | Update an existing user |
| DELETE | `/users/<id>` | Delete a user by ID |
Example Usage (with curl or Postman) => Create a user:
curl -X POST http://localhost:5000/users \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "email": "[email protected]", "password": "secret123"}'
Get all users:
curl -X GET http://localhost:5000/users
Update a User (PUT) Replace with the actual user ID:
curl -X PUT http://localhost:5000/users/<id> \
-H "Content-Type: application/json" \
-d '{"name": "Updated Name", "email": "[email protected]", "password": "newpassword123"}'
Delete a User (DELETE) Replace with the actual user ID:
curl -X DELETE http://localhost:5000/users/<id>
If you'd like, I can also generate a downloadable README.md
file for you. Would you want that?