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();
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…