When attempting to upload a file with duplicate value of a metadata field that has a unique index on it, Spring framework’s GridFsTemplate throws a MongoWriteException (E11000 duplicate key error) but also leaves orphan chunk documents in .chunks collection. The number of orphan chunk document is equal to number of normal chunk documents created when the file is uploaded the first time pointing to the fact that the whole file is uploaded before the unique index constraint is evaluated.
I have not found any official explanation about this behavior of GridFS and therefore, i think this maybe a bug as this causes needless bandwidth and storage waste which can be easily avoided.
Can anyone please help me see if my conclusion is correct or if anything can be done to avoid orphan chunk creation issue.