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 arrayscreenShot
in 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,screenShot
The 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
-
Data serialization problem: Every time a new picture is saved, the system obtains the existing one from the database
screenShot
Field content, but not properly handled JSON strings that may already exist - 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
- Lack of data cleaning: The system did not clean and verify the existing data read from the database, resulting in continuous accumulation of problems
- 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
- Strict format verification before data storage
- Add unit test validation data serialization and deserialization logic
- 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.