[英]How to perform Update operations in GridFS (using Java)?
I am using Mongo-Java-Driver 2.13
I stored a PDF file ( size 30mb ) in GridFS . 我正在使用Mongo-Java-Driver 2.13
我在GridFS中存储了一个PDF文件( 大小为30mb) 。 I am able to perform insertion, deletion and find operation easily. 我能够轻松地执行插入,删除和查找操作。
MongoClient mongo = new MongoClient("localhost", 27017);
DB db = mongo.getDB("testDB");
File pdfFile = new File("/home/dev/abc.pdf");
GridFS gfs = new GridFS(db,"books");
GridFSInputFile inputFile = gfs.createFile(pdfFile);
inputFile.setId("101");
inputFile.put("title", "abc");
inputFile.put("author", "xyz");
inputFile.save();
data is persisted in books.files
and books.chunks
collections. 数据保存在books.files
和books.chunks
集合中。 Now I want to update : 现在我要更新 :
How to perform these Update operations for Case 1 in GridFS ? 如何在GridFS中对案例1执行这些更新操作?
I came to know that I need to maintain multiple versions of my files and pick up the right version. 我开始知道我需要维护我的文件的多个版本并选择正确的版本。 Can anybody put some clarity on it? 有人可以说清楚吗?
Edit : 编辑 :
I can update metadata(title, author) easily. 我可以轻松更新元数据(标题,作者)。
GridFSDBFile outputFile = gfs.findOne(new BasicDBObject("_id", 101));
BasicDBObject updatedMetadata = new BasicDBObject();
updatedMetadata.put("name", "PG");
updatedMetadata.put("age", 22);
outputFile.setMetaData(newMetadata);
outputFile.save();
In GridFS you are not removing/deleting a single document but actually a bunch of documents (files are split into chunks and each chunk is a separate document). 在GridFS中,您不是删除/删除单个文档,而是实际上是一堆文档(文件被拆分为块,每个块都是一个单独的文档)。 That means replacing a file is simply not possible in an atomic manner. 这意味着以原子方式替换文件根本不可能。
What you can do instead is: 你可以做的是:
GridFS is kind of a hackish feature. GridFS是一种hackish功能。 It is often better to just use a separate fileserver with a real filesystem to store the file content and only store the metadata in MongoDB. 通常最好只使用具有真实文件系统的单独文件服务器来存储文件内容,并仅将元数据存储在MongoDB中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.