{"id":29190687,"url":"https://github.com/ishanoshada/flask-mongodb-server","last_synced_at":"2026-04-14T19:31:53.578Z","repository":{"id":302327414,"uuid":"1012054757","full_name":"Ishanoshada/Flask-Mongodb-Server","owner":"Ishanoshada","description":"A Flask-based HTTP server for MongoDB operations (create, insert, find, delete, update) with ESP32 client support using Arduino and MicroPython.","archived":false,"fork":false,"pushed_at":"2025-07-01T19:18:56.000Z","size":20,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-07-01T19:39:37.902Z","etag":null,"topics":["arduino","esp32","flask","iot","micropython","mongodb","nosql","pymongo","python"],"latest_commit_sha":null,"homepage":"https://flask-mongodb-server.vercel.app","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Ishanoshada.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-07-01T18:32:16.000Z","updated_at":"2025-07-01T19:18:59.000Z","dependencies_parsed_at":"2025-07-01T19:49:41.477Z","dependency_job_id":null,"html_url":"https://github.com/Ishanoshada/Flask-Mongodb-Server","commit_stats":null,"previous_names":["ishanoshada/flask-mongodb-server"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Ishanoshada/Flask-Mongodb-Server","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ishanoshada%2FFlask-Mongodb-Server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ishanoshada%2FFlask-Mongodb-Server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ishanoshada%2FFlask-Mongodb-Server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ishanoshada%2FFlask-Mongodb-Server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Ishanoshada","download_url":"https://codeload.github.com/Ishanoshada/Flask-Mongodb-Server/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ishanoshada%2FFlask-Mongodb-Server/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263052430,"owners_count":23406106,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arduino","esp32","flask","iot","micropython","mongodb","nosql","pymongo","python"],"created_at":"2025-07-02T00:10:49.766Z","updated_at":"2026-04-14T19:31:53.567Z","avatar_url":"https://github.com/Ishanoshada.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Flask-MongoDB-Server\n\nThis project provides a Flask-based web server that acts as a general-purpose interface for MongoDB operations. It allows any client (e.g., microcontrollers like ESP32, web applications, or scripts) to perform MongoDB operations (create, insert, find, delete, update) via HTTP POST, PUT, and DELETE requests. The server is designed to be flexible and can be used with any device or application capable of sending HTTP requests with JSON payloads.\n\n## Features\n- **Flask Server**: Exposes a RESTful endpoint (`/mongodb`) to handle MongoDB operations.\n- **Supported Operations**:\n  - `create` (POST): Create a new database and collection if they don’t exist.\n  - `insert` (POST): Add one or more documents to a collection.\n  - `find` (POST): Retrieve documents matching a filter.\n  - `delete` (DELETE): Delete documents matching a filter.\n  - `update` (PUT): Update documents with a specified filter and update operation.\n- **Client Flexibility**: Compatible with any client that can send HTTP POST, PUT, or DELETE requests with JSON payloads, including microcontrollers (e.g., ESP32), web apps, or command-line tools.\n- **Secure Transmission**: Queries and MongoDB URI are sent in JSON payloads, ensuring safe data transfer when using HTTPS.\n- **Example Clients**: Includes an ESP32 sketch, MicroPython examples, and a Python test script.\n\n## Project Structure\n- `/index/api.py`: Flask server script for MongoDB operations.\n- `esp32_mongodb.ino`: Example ESP32 Arduino sketch for sending queries.\n- `esp32_arduino_examples.ino`: Arduino example with functions for all MongoDB operations.\n- `esp32_micropython_examples.py`: MicroPython example with functions for all MongoDB operations.\n- `test_mongodb_server.py`: Python script to test the server with all operations.\n- `requirements.txt`: Python dependencies for the Flask server and test script.\n- `README.md`: Project documentation (this file).\n- `.gitignore`: Excludes unnecessary files from version control.\n\n## Prerequisites\n### Flask Server\n- Python 3.8 or higher\n- MongoDB instance (e.g., MongoDB Atlas)\n- Dependencies listed in `requirements.txt`\n\n### Example ESP32 Client (Optional)\n- ESP32 board (e.g., ESP32 DevKit)\n- Arduino IDE with ESP32 board support or MicroPython firmware\n- Arduino libraries: `WiFi`, `HTTPClient`, `ArduinoJson` (install via Library Manager)\n- MicroPython libraries: `urequests` (install via `upip`)\n\n### Test Script (Optional)\n- Python 3.8 or higher\n- `requests` library (included in `requirements.txt`)\n\n## Setup Instructions\n### Flask Server\n1. Clone the repository:\n   ```bash\n   git clone https://github.com/ishanoshada/Flask-Mongodb-Server.git\n   cd Flask-Mongodb-Server\n   ```\n2. Install dependencies:\n   ```bash\n   pip install -r requirements.txt\n   ```\n3. Run the Flask server:\n   ```bash\n   python ./index/api.py\n   ```\n   The server will run on `http://\u003cyour-server-ip\u003e:5000`.\n\n### Example ESP32 Client (Optional)\n#### Arduino (C/C++)\n1. Open `esp32_arduino_examples.ino` or `esp32_mongodb.ino` in the Arduino IDE.\n2. Update the following variables:\n   - `ssid`: Your WiFi network name.\n   - `password`: Your WiFi password.\n   - `serverUrl`: The Flask server URL (e.g., `http://\u003cyour-server-ip\u003e:5000/mongodb`).\n   - `mongoUri`: Your MongoDB URI (e.g., `mongodb://user:pass@host:port/`).\n3. Install required libraries in Arduino IDE:\n   - Go to **Sketch \u003e Include Library \u003e Manage Libraries**.\n   - Install `ArduinoJson` (by Benoit Blanchon).\n4. Upload the sketch to your ESP32 board.\n5. For `esp32_arduino_examples.ino`, call the desired function (e.g., `sendCreateRequest()`) in `loop()`.\n\n#### MicroPython\n1. Install MicroPython on your ESP32 (see `micropython.org`).\n2. Install the `urequests` library:\n   ```bash\n   import upip\n   upip.install(\"micropython-urequests\")\n   ```\n3. Upload `esp32_micropython_examples.py` to your ESP32 using a tool like `ampy` or `rshell`.\n4. Update `SSID`, `PASSWORD`, `SERVER_URL`, and `MONGO_URI` in the script.\n5. Run the script and call the desired function (e.g., `send_create_request()`) via the MicroPython REPL.\n\n### Test Script\n1. Ensure the Flask server is running.\n2. Update `test_mongodb_server.py` with your server URL and MongoDB URI:\n   - `SERVER_URL`: Set to the Flask server URL (e.g., `http://\u003cyour-server-ip\u003e:5000/mongodb`).\n   - `MONGO_URI`: Set to your MongoDB URI (e.g., `mongodb://user:pass@host:port/`).\n3. Run the test script:\n   ```bash\n   python test_mongodb_server.py\n   ```\n\n## Constructing Requests\nThe server accepts HTTP POST, PUT, and DELETE requests at `/mongodb` with a JSON payload containing `uri` and `query`. The `query` field specifies the MongoDB operation and its parameters. The JSON payload does not require URL-encoding since it is sent in the request body, but the MongoDB URI should be properly formatted.\n\n### Request Structure\n- **Endpoint**: `http://\u003cyour-server-ip\u003e:5000/mongodb`\n- **Headers**: `Content-Type: application/json`\n- **Body**: JSON object with:\n  - `uri`: MongoDB connection string (e.g., `mongodb://user:pass@host:port/`).\n  - `query`: Object containing `db`, `collection`, `operation`, and operation-specific fields (e.g., `data` for insert, `filter` for find/delete, `filter` and `update` for update).\n\n## Example Requests\nThe following examples show the JSON payloads for each operation and their expected responses. Use these payloads in the request body for POST, PUT, or DELETE requests to `/mongodb`.\n\n### Create (POST)\n- **JSON Payload**:\n  ```json\n  {\n    \"uri\": \"mongodb://user:pass@host:port/\",\n    \"query\": {\n      \"db\": \"test\",\n      \"collection\": \"data\",\n      \"operation\": \"create\"\n    }\n  }\n  ```\n- **Response**:\n  ```json\n  {\"message\": \"Collection data created in database test\"}\n  ```\n  or, if the collection already exists:\n  ```json\n  {\"message\": \"Collection data already exists in database test\"}\n  ```\n- **cURL Command**:\n  ```bash\n  curl -X POST -H \"Content-Type: application/json\" -d '{\"uri\":\"mongodb://user:pass@host:port/\",\"query\":{\"db\":\"test\",\"collection\":\"data\",\"operation\":\"create\"}}' http://\u003cserver-ip\u003e:5000/mongodb\n  ```\n\n### Insert (Single Document, POST)\n- **JSON Payload**:\n  ```json\n  {\n    \"uri\": \"mongodb://user:pass@host:port/\",\n    \"query\": {\n      \"db\": \"test\",\n      \"collection\": \"data\",\n      \"operation\": \"insert\",\n      \"data\": {\"name\": \"John\", \"age\": 25}\n    }\n  }\n  ```\n- **Response**:\n  ```json\n  {\"inserted_id\": \"507f1f77bcf86cd799439011\"}\n  ```\n- **cURL Command**:\n  ```bash\n  curl -X POST -H \"Content-Type: application/json\" -d '{\"uri\":\"mongodb://user:pass@host:port/\",\"query\":{\"db\":\"test\",\"collection\":\"data\",\"operation\":\"insert\",\"data\":{\"name\":\"John\",\"age\":25}}}' http:/\n/\u003cserver-ip\u003e:5000/mongodb\n  ```\n\n### Insert (Multiple Documents, POST)\n- **JSON Payload**:\n  ```json\n  {\n    \"uri\": \"mongodb://user:pass@host:port/\",\n    \"query\": {\n      \"db\": \"test\",\n      \"collection\": \"data\",\n      \"operation\": \"insert\",\n      \"data\": [\n        {\"name\": \"John\", \"age\": 25},\n        {\"name\": \"Jane\", \"age\": 30}\n      ]\n    }\n  }\n  ```\n- **Response**:\n  ```json\n  {\"inserted_ids\": [\"507f1f77bcf86cd799439011\", \"507f1f77bcf86cd799439012\"]}\n  ```\n- **cURL Command**:\n  ```bash\n  curl -X POST -H \"Content-Type: application/json\" -d '{\"uri escult=\"mongodb://user:pass@host:port/\",\"query\":{\"db\":\"test\",\"collection\":\"data\",\"operation\":\"insert\",\"data\":[{\"name\":\"John\",\"age\":25},{\"name\":\"Jane\",\"age\":30}]}}' http://\u003cserver-ip\u003e:5000/mongodb\n  ```\n\n### Find (POST)\n- **JSON Payload**:\n  ```json\n  {\n    \"uri\": \"mongodb://user:pass@host:port/\",\n    \"query\": {\n      \"db\": \"test\",\n      \"collection\": \"data\",\n      \"operation\": \"find\",\n      \"filter\": {\"name\": \"John\"}\n    }\n  }\n  ```\n- **Response**:\n  ```json\n  {\"results\": [{\"_id\": \"507f1f77bcf86cd799439011\", \"name\": \"John\", \"age\": 25}]}\n  ```\n- **cURL Command**:\n  ```bash\n  curl -X POST -H \"Content-Type: application/json\" -d '{\"uri\":\"mongodb://user:pass@host:port/\",\"query\":{\"db\":\"test\",\"collection\":\"data\",\"operation\":\"find\",\"filter\":{\"name\":\"John\"}}}' http://\u003cserver-ip\u003e:5000/mongodb\n  ```\n\n### Delete (DELETE)\n- **JSON Payload**:\n  ```json\n  {\n    \"uri\": \"mongodb://user:pass@host:port/\",\n    \"query\": {\n      \"db\": \"test\",\n      \"collection\": \"data\",\n      \"operation\": \"delete\",\n      \"filter\": {\"name\": \"John\"}\n    }\n  }\n  ```\n- **Response**:\n  ```json\n  {\"deleted_count\": 2}\n  ```\n- **cURL Command**:\n  ```bash\n  curl -X DELETE -H \"Content-Type: application/json\" -d '{\"uri\":\"mongodb://user:pass@host:port/\",\"query\":{\"db\":\"test\",\"collection\":\"data\",\"operation\":\"delete\",\"filter\":{\"name\":\"John\"}}}' http://\u003cserver-ip\u003e:5000/mongodb\n  ```\n\n### Update (PUT)\n- **JSON Payload**:\n  ```json\n  {\n    \"uri\": \"mongodb://user:pass@host:port/\",\n    \"query\": {\n      \"db\": \"test\",\n      \"collection\": \"data\",\n      \"operation\": \"update\",\n      \"filter\": {\"name\": \"John\"},\n      \"update\": {\"$set\": {\"age\": 30}}\n    }\n  }\n  ```\n- **Response**:\n  ```json\n  {\"matched_count\": 2, \"modified_count\": 2}\n  ```\n- **cURL Command**:\n  ```bash\n  curl -X PUT -H \"Content-Type: application/json\" -d '{\"uri\":\"mongodb://user:pass@host:port/\",\"query\":{\"db\":\"test\",\"collection\":\"data\",\"operation\":\"update\",\"filter\":{\"name\":\"John\"},\"update\":{\"$set\":{\"age\":30}}}}' http://\u003cserver-ip\u003e:5000/mongodb\n  ```\n\n## Example Clients\nThe project includes example clients to demonstrate how to interact with the Flask server’s `/mongodb` endpoint. Below are code snippets for an ESP32 using Arduino (C/C++) and MicroPython, covering all supported operations (`create`, `insert`, `find`, `delete`, `update`). Full example code is provided in `esp32_arduino_examples.ino` and `esp32_micropython_examples.py`.\n\n### Arduino (C/C++) Client for ESP32\nThe following snippets (from `esp32_arduino_examples.ino`) use the `WiFi`, `HTTPClient`, and `ArduinoJson` libraries to send HTTP POST, PUT, and DELETE requests for each operation. Update `ssid`, `password`, `serverUrl`, and `mongoUri` before use.\n\n#### Create (POST)\n```cpp\nvoid sendCreateRequest() {\n  HTTPClient http;\n  StaticJsonDocument\u003c300\u003e doc;\n  doc[\"uri\"] = mongoUri;\n  JsonObject query = doc.createNestedObject(\"query\");\n  query[\"db\"] = \"test\";\n  query[\"collection\"] = \"data\";\n  query[\"operation\"] = \"create\";\n\n  String payload;\n  serializeJson(doc, payload);\n  http.begin(serverUrl);\n  http.addHeader(\"Content-Type\", \"application/json\");\n  int httpCode = http.POST(payload);\n  if (httpCode \u003e 0) {\n    Serial.println(\"Create Response: \" + http.getString());\n  } else {\n    Serial.println(\"Create Failed: \" + http.errorToString(httpCode));\n  }\n  http.end();\n}\n```\n\n#### Insert (Single Document, POST)\n```cpp\nvoid sendInsertSingleRequest() {\n  HTTPClient http;\n  StaticJsonDocument\u003c300\u003e doc;\n  doc[\"uri\"] = mongoUri;\n  JsonObject query = doc.createNestedObject(\"query\");\n  query[\"db\"] = \"test\";\n  query[\"collection\"] = \"data\";\n  query[\"operation\"] = \"insert\";\n  JsonObject data = query.createNestedObject(\"data\");\n  data[\"name\"] = \"John\";\n  data[\"age\"] = 25;\n\n  String payload;\n  serializeJson(doc, payload);\n  http.begin(serverUrl);\n  http.addHeader(\"Content-Type\", \"application/json\");\n  int httpCode = http.POST(payload);\n  if (httpCode \u003e 0) {\n    Serial.println(\"Insert Single Response: \" + http.getString());\n  } else {\n    Serial.println(\"Insert Single Failed: \" + http.errorToString(httpCode));\n  }\n  http.end();\n}\n```\n\n#### Insert (Multiple Documents, POST)\n```cpp\nvoid sendInsertMultipleRequest() {\n  HTTPClient http;\n  StaticJsonDocument\u003c300\u003e doc;\n  doc[\"uri\"] = mongoUri;\n  JsonObject query = doc.createNestedObject(\"query\");\n  query[\"db\"] = \"test\";\n  query[\"collection\"] = \"data\";\n  query[\"operation\"] = \"insert\";\n  JsonArray data = query.createNestedArray(\"data\");\n  JsonObject doc1 = data.createNestedObject();\n  doc1[\"name\"] = \"John\";\n  doc1[\"age\"] = 25;\n  JsonObject doc2 = data.createNestedObject();\n  doc2[\"name\"] = \"Jane\";\n  doc2[\"age\"] = 30;\n\n  String payload;\n  serializeJson(doc, payload);\n  http.begin(serverUrl);\n  http.addHeader(\"Content-Type\", \"application/json\");\n  int httpCode = http.POST(payload);\n  if (httpCode \u003e 0) {\n    Serial.println(\"Insert Multiple Response: \" + http.getString());\n  } else {\n    Serial.println(\"Insert Multiple Failed: \" + http.errorToString(httpCode));\n  }\n  http.end();\n}\n```\n\n#### Find (POST)\n```cpp\nvoid sendFindRequest() {\n  HTTPClient http;\n  StaticJsonDocument\u003c300\u003e doc;\n  doc[\"uri\"] = mongoUri;\n  JsonObject query = doc.createNestedObject(\"query\");\n  query[\"db\"] = \"test\";\n  query[\"collection\"] = \"data\";\n  query[\"operation\"] = \"find\";\n  JsonObject filter = query.createNestedObject(\"filter\");\n  filter[\"name\"] = \"John\";\n\n  String payload;\n  serializeJson(doc, payload);\n  http.begin(serverUrl);\n  http.addHeader(\"Content-Type\", \"application/json\");\n  int httpCode = http.POST(payload);\n  if (httpCode \u003e 0) {\n    Serial.println(\"Find Response: \" + http.getString());\n  } else {\n    Serial.println(\"Find Failed: \" + http.errorToString(httpCode));\n  }\n  http.end();\n}\n```\n\n#### Delete (DELETE)\n```cpp\nvoid sendDeleteRequest() {\n  HTTPClient http;\n  StaticJsonDocument\u003c300\u003e doc;\n  doc[\"uri\"] = mongoUri;\n  JsonObject query = doc.createNestedObject(\"query\");\n  query[\"db\"] = \"test\";\n  query[\"collection\"] = \"data\";\n  query[\"operation\"] = \"delete\";\n  JsonObject filter = query.createNestedObject(\"filter\");\n  filter[\"name\"] = \"John\";\n\n  String payload;\n  serializeJson(doc, payload);\n  http.begin(serverUrl);\n  http.addHeader(\"Content-Type\", \"application/json\");\n  int httpCode = http.sendRequest(\"DELETE\", payload);\n  if (httpCode \u003e 0) {\n    Serial.println(\"Delete Response: \" + http.getString());\n  } else {\n    Serial.println(\"Delete Failed: \" + http.errorToString(httpCode));\n  }\n  http.end();\n}\n```\n\n#### Update (PUT)\n```cpp\nvoid sendUpdateRequest() {\n  HTTPClient http;\n  StaticJsonDocument\u003c300\u003e doc;\n  doc[\"uri\"] = mongoUri;\n  JsonObject query = doc.createNestedObject(\"query\");\n  query[\"db\"] = \"test\";\n  query[\"collection\"] = \"data\";\n  query[\"operation\"] = \"update\";\n  JsonObject filter = query.createNestedObject(\"filter\");\n  filter[\"name\"] = \"John\";\n  JsonObject update = query.createNestedObject(\"update\");\n  JsonObject setData = update.createNestedObject(\"$set\");\n  setData[\"age\"] = 30;\n\n  String payload;\n  serializeJson(doc, payload);\n  http.begin(serverUrl);\n  http.addHeader(\"Content-Type\", \"application/json\");\n  int httpCode = http.PUT(payload);\n  if (httpCode \u003e 0) {\n    Serial.println(\"Update Response: \" + http.getString());\n  } else {\n    Serial.println(\"Update Failed: \" + http.errorToString(httpCode));\n  }\n  http.end();\n}\n```\n\n**Setup**:\n1. Install libraries in Arduino IDE: `WiFi`, `HTTPClient`, `ArduinoJson` (via Library Manager).\n2. Open `esp32_arduino_examples.ino` and update `ssid`, `password`, `serverUrl`, and `mongoUri`.\n3. Call the desired function (e.g., `sendCreateRequest()`) in `loop()` or `setup()`.\n4. Upload to your ESP32 board.\n5. Monitor Serial output (115200 baud) for responses.\n\n### MicroPython Client for ESP32\nThe following snippets (from `esp32_micropython_examples.py`) use the `urequests` library to send HTTP POST, PUT, and DELETE requests for each operation. Update `SSID`, `PASSWORD`, `SERVER_URL`, and `MONGO_URI` before use.\n\n#### Create (POST)\n```python\ndef send_create_request():\n    import urequests\n    import ujson\n    payload = {\n        \"uri\": MONGO_URI,\n        \"query\": {\n            \"db\": \"test\",\n            \"collection\": \"data\",\n            \"operation\": \"create\"\n        }\n    }\n    try:\n        response = urequests.post(SERVER_URL, json=payload)\n        print(\"Create Response:\", response.text)\n        response.close()\n    except Exception as e:\n        print(\"Create Failed:\", e)\n```\n\n#### Insert (Single Document, POST)\n```python\ndef send_insert_single_request():\n    import urequests\n    import ujson\n    payload = {\n        \"uri\": MONGO_URI,\n        \"query\": {\n            \"db\": \"test\",\n            \"collection\": \"data\",\n            \"operation\": \"insert\",\n            \"data\": {\"name\": \"John\", \"age\": 25}\n        }\n    }\n    try:\n        response = urequests.post(SERVER_URL, json=payload)\n        print(\"Insert Single Response:\", response.text)\n        response.close()\n    except Exception as e:\n        print(\"Insert Single Failed:\", e)\n```\n\n#### Insert (Multiple Documents, POST)\n```python\ndef send_insert_multiple_request():\n    import urequests\n    import ujson\n    payload = {\n        \"uri\": MONGO_URI,\n        \"query\": {\n            \"db\": \"test\",\n            \"collection\": \"data\",\n            \"operation\": \"insert\",\n            \"data\": [\n                {\"name\": \"John\", \"age\": 25},\n                {\"name\": \"Jane\", \"age\": 30}\n            ]\n        }\n    }\n    try:\n        response = urequests.post(SERVER_URL, json=payload)\n        print(\"Insert Multiple Response:\", response.text)\n        response.close()\n    except Exception as e:\n        print(\"Insert Multiple Failed:\", e)\n```\n\n#### Find (POST)\n```python\ndef send_find_request():\n    import urequests\n    import ujson\n    payload = {\n        \"uri\": MONGO_URI,\n        \"query\": {\n            \"db\": \"test\",\n            \"collection\": \"data\",\n            \"operation\": \"find\",\n            \"filter\": {\"name\": \"John\"}\n        }\n    }\n    try:\n        response = urequests.post(SERVER_URL, json=payload)\n        print(\"Find Response:\", response.text)\n        response.close()\n    except Exception as e:\n        print(\"Find Failed:\", e)\n```\n\n#### Delete (DELETE)\n```python\ndef send_delete_request():\n    import urequests\n    import ujson\n    payload = {\n        \"uri\": MONGO_URI,\n        \"query\": {\n            \"db\": \"test\",\n            \"collection\": \"data\",\n            \"operation\": \"delete\",\n            \"filter\": {\"name\": \"John\"}\n        }\n    }\n    try:\n        response = urequests.request(\"DELETE\", SERVER_URL, json=payload)\n        print(\"Delete Response:\", response.text)\n        response.close()\n    except Exception as e:\n        print(\"Delete Failed:\", e)\n```\n\n#### Update (PUT)\n```python\ndef send_update_request():\n    import urequests\n    import ujson\n    payload = {\n        \"uri\": MONGO_URI,\n        \"query\": {\n            \"db\": \"test\",\n            \"collection\": \"data\",\n            \"operation\": \"update\",\n            \"filter\": {\"name\": \"John\"},\n            \"update\": {\"$set\": {\"age\": 30}}\n        }\n    }\n    try:\n        response = urequests.request(\"PUT\", SERVER_URL, json=payload)\n        print(\"Update Response:\", response.text)\n        response.close()\n    except Exception as e:\n        print(\"Update Failed:\", e)\n```\n\n**Setup**:\n1. Install MicroPython on your ESP32 (see `micropython.org`).\n2. Install the `urequests` library:\n   ```bash\n   import upip\n   upip.install(\"micropython-urequests\")\n   ```\n3. Upload `esp32_micropython_examples.py` to your ESP32 using `ampy`, `rshell`, or Thonny.\n4. Update `SSID`, `PASSWORD`, `SERVER_URL`, and `MONGO_URI`.\n5. Call the desired function (e.g., `send_create_request()`) via the MicroPython REPL.\n6. Monitor console output for responses.\n\n### Python Test Script\nThe project includes `test_mongodb_server.py`, which tests all operations (`create`, `insert`, `find`, `delete`, `update`) using the `requests` library. See the **Test Script** section for setup and execution instructions.\n\n## Security Notes\n- **MongoDB URI**: Contains sensitive credentials. Use HTTPS or a secure network to protect it. Consider storing the URI in a `.env` file (excluded via `.gitignore`) and loading it in your scripts.\n- **Network**: Ensure the Flask server’s IP is whitelisted in your MongoDB instance (e.g., MongoDB Atlas).\n- **Query Size**: For complex queries, adjust the `StaticJsonDocument` size in Arduino sketches (default is 300 bytes) or equivalent in other clients.\n\n## Troubleshooting\n- **Flask Server**:\n  - Verify the MongoDB URI and ensure the server is reachable.\n  - Check terminal output for errors when running `/index/api.py`.\n- **ESP32 Client (Arduino)**:\n  - Confirm WiFi credentials and server URL are correct.\n  - Monitor Serial output (115200 baud) for error messages.\n  - Ensure the `ArduinoJson` library is installed.\n- **ESP32 Client (MicroPython)**:\n  - Verify WiFi credentials, server URL, and `urequests` installation.\n  - Check console output for errors.\n  - Ensure the ESP32 has sufficient memory for complex queries.\n- **Test Script**:\n  - Ensure the `requests` library is installed (`pip install requests`).\n  - Verify the server URL and MongoDB URI in `test_mongodb_server.py`.\n  - Check for network connectivity or firewall issues if requests fail.\n- **Other Clients**: Ensure the JSON payload is correctly formatted and the MongoDB URI is accessible.\n\n## Contributing\nContributions are welcome! Please submit a pull request or open an issue on GitHub.\n\n## Author\n- GitHub: [ishanoshada](https://github.com/ishanoshada)\n\n\n**Repository Views** ![Views](https://profile-counter.glitch.me/flask-server-mongodb/count.svg)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fishanoshada%2Fflask-mongodb-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fishanoshada%2Fflask-mongodb-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fishanoshada%2Fflask-mongodb-server/lists"}