SoFunction
Updated on 2025-04-25

About MongoDB image URL storage problem and solution

MongoDB image URL storage problem

Project Scenario

When developing an online exam system, the front-end needs to submit screenshots of students' answers to the back-end, and the back-end uses MinIO to store pictures and save the image URL to the MongoDB database.

The system needs to support multiple submissions of images and store all image URLs in MongoDB as an arrayscreenShotin the field.

Problem description

When storing a small number of images, the URL displays normally (eg@http://10.100.157.90:9200/test-bucket/exam_1.png

But when the number of pictures increases,screenShotThe contents of the field store become multi-layer nested JSON strings containing a large number of escape characters and slashes

For example:

"screenShot": ["{\"screenShot\": [\"{\\\"screenShot\\\": [\\\"{\\\\\\\"screenShot\\\\\\\": [\\\\\\\"...\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"]}\"]}"]

Cause analysis

  1. Data serialization problem: Every time a new picture is saved, the system obtains the existing one from the databasescreenShotField content, but not properly handled JSON strings that may already exist
  2. Nested serialization: Each time the entire array is updated, it is incorrectly serialized to a JSON string and then added to the array as a new element
  3. Lack of data cleaning: The system did not clean and verify the existing data read from the database, resulting in continuous accumulation of problems
  4. MongoDB is not operating properly: When updating MongoDB fields, an inappropriate serialization method may be used

Solution

1. Use MongoDB's array operator (such as$push) Update the array directly, rather than replacing the entire field

2. Or useData cleaning(Optional, not recommended)

Preventive measures

  1. Strict format verification before data storage
  2. Add unit test validation data serialization and deserialization logic
  3. Regularly check and clean existing data in the database

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.