Append data to existing gridfs file

后端 未结 1 558
日久生厌
日久生厌 2020-12-19 15:53

As i can see java mongo driver does not provide capability to get OutputStream from existing gridFS file com.mongodb.gridfs.GridFSFile

I ha

相关标签:
1条回答
  • 2020-12-19 16:23

    GridFS is not a core feature of MongoDB but a convention of storing binary data with accompanying metadata. You should be able to modify any document in fs.chunks collection in a usual manner while keeping corresponding document in fs.files intact. The main problem will be recalculating MD5 checksum, but AFAIK it isn't used anywhere and is just a "free" bonus. Anyway it's still possible for appending-only modification (see MD5 digest of a resumed download).

    So to append to an existing GridFS file you need to locate corresponding document in fs.files. Then depending on last chunk filling ratio (length % chunkSize == 0) you either rewrite the last chunk document in fs.chunks respecting the chunkSize, and/or simply add new chunk(s) with incrementing n field. Next update length in fs.files and possibly other metadata.

    0 讨论(0)
提交回复
热议问题