Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

I have to create GridFSInputFile directly or use gridFs.createFile() method.

Is it a lack of java driver or a restriction of gridfs ?

Could you suggest any workaround except create new file/delete old one ?

Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
422 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...