How to perform Update operations in GridFS (using Java)?

半世苍凉 提交于 2019-11-30 06:01:30

问题


I am using Mongo-Java-Driver 2.13 I stored a PDF file (size 30mb) in GridFS. 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. Now I want to update :

  • case 1: pdf file
  • case 2: title or author

How to perform these Update operations for Case 1 in GridFS ?

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();

回答1:


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). That means replacing a file is simply not possible in an atomic manner.

What you can do instead is:

  1. insert a new file with a new name
  2. after this happened (use the replica acknowledged write-concern), update all references to the old file to point to the new one
  3. after you got a confirmation for this, you can delete the old file

GridFS is kind of a hackish feature. 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.



来源:https://stackoverflow.com/questions/29515327/how-to-perform-update-operations-in-gridfs-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!