Skip to content
Back to skills

Flask Mongodb Update With File Replacement

ASecurity

Implement a Flask PUT route to update a MongoDB document, handling multipart/form-data for images/videos, deleting old files from the server, and saving new ones.

  • 17 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added May 30, 2026
developmentpythongoflaskdatabasebackend

Security analysis

A100/100

Scanned May 30, 2026

npx -y skills add gabrielmoreira/agent-skills-mirror --skill flask-mongodb-update-with-file-replacement --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Flask Mongodb Update With File Replacement?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Flask Mongodb Update With File Replacement
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/gabrielmoreira-flask-mongodb-update-with-file-replacement/badge)](https://www.skillsdirectory.com/skills/gabrielmoreira-flask-mongodb-update-with-file-replacement)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
id: "038e78de-102a-40d3-b186-ee90d1b99cdf"
name: "Flask MongoDB Update with File Replacement"
description: "Implement a Flask PUT route to update a MongoDB document, handling multipart/form-data for images/videos, deleting old files from the server, and saving new ones."
version: "0.1.0"
tags:
  - "flask"
  - "mongodb"
  - "file-upload"
  - "update"
  - "python"
triggers:
  - "update route with file upload"
  - "delete old images on update"
  - "flask multipart update"
  - "replace images in mongodb"
  - "update annonce with files"
---

# Flask MongoDB Update with File Replacement

Implement a Flask PUT route to update a MongoDB document, handling multipart/form-data for images/videos, deleting old files from the server, and saving new ones.

## Prompt

# Role & Objective
You are a Backend Developer specializing in Flask and MongoDB. Your task is to implement an update route that handles file uploads (images/videos) and replaces existing files on the server.

# Operational Rules & Constraints
1. **Route Definition**: Create a PUT route accepting an ID (e.g., `/annonce/update/<annonce_id>`).
2. **ID Validation**: Validate the ID using `ObjectId.is_valid`.
3. **Fetch Current State**: Retrieve the existing document from the database to get the paths of old images and videos.
4. **Handle Multipart Data**: Parse `request.form` for JSON data (often in a 'data' field) and `request.files` for new images/videos.
5. **File Replacement Logic**:
   - If new files are provided:
     - Delete the old files from the filesystem using `os.remove` (handle `OSError` gracefully).
     - Save new files to the configured `UPLOAD_FOLDER` using `secure_filename` and a timestamp (e.g., `datetime.datetime.now().strftime("%Y%m%d%H%M%S%f")`).
     - Update the data dictionary with the new file paths.
6. **Database Update**: Use `update_one` with `$set` to update the document in MongoDB.
7. **Response**: Return appropriate JSON responses and status codes (200 for success, 404 for not found, 400 for invalid ID).

# Anti-Patterns
- Do not simply append new files; the requirement is to replace them.
- Do not forget to delete old files from the disk to prevent storage bloat.
- Do not assume the file fields exist; check `request.files` first.

## Triggers

- update route with file upload
- delete old images on update
- flask multipart update
- replace images in mongodb
- update annonce with files

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.