将数据附加到现有gridfs文件 [英] Append data to existing gridfs file
问题描述
我可以看到java mongo驱动程序无法从现有的gridFS文件中获取 OutputStream
com.mongodb.gridfs.GridFSFile
As i can see java mongo driver does not provide capability to get OutputStream
from existing gridFS file com.mongodb.gridfs.GridFSFile
我必须直接创建 GridFSInputFile
或使用 gridFs.createFile( )
方法。
I have to create GridFSInputFile
directly or use gridFs.createFile()
method.
缺少java驱动程序还是限制gridfs?
Is it a lack of java driver or a restriction of gridfs ?
除了创建新文件/删除旧文件外,您能建议任何解决方法吗?
Could you suggest any workaround except create new file/delete old one ?
谢谢
推荐答案
GridFS不是MongoDB的核心功能,而是存储带有伴随元数据的二进制数据的约定。您应该能够以通常的方式修改 fs.chunks
集合中的任何文档,同时在 fs.files
完好无损。主要问题是重新计算MD5校验和,但AFAIK它不会在任何地方使用,只是一个免费奖金。无论如何,仍然可以进行仅附加修改(参见恢复下载的MD5摘要)。
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).
因此要附加到现有的GridFS文件,您需要在 fs.files
中找到相应的文档。然后根据最后的块填充率(长度
% chunkSize
== 0),您要么重写<中的最后一个块文档code> fs.chunks 尊重 chunkSize
,和/或简单地添加新增的块 n
字段。下一次更新长度
在 fs.files
中,可能还有其他元数据。
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.
这篇关于将数据附加到现有gridfs文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!