Skip to content

Instantly share code, notes, and snippets.

@kalharbi
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kalharbi/ac181291ccf7e601927f to your computer and use it in GitHub Desktop.
Save kalharbi/ac181291ccf7e601927f to your computer and use it in GitHub Desktop.
Retrieve zip file from MongoDB GridFS and save it in a directory
MongoClient mongoClient = new MongoClient("localhost", 27017);
DB db = mongoClient.getDB("archives");
GridFS gridGfs = new GridFS(db, "fs");
// search by metadata
BasicDBObject query = new BasicDBObject("metadata.n", "myZipFile")
.append("metadata.ver", "1.2.6");
GridFSDBFile zipFile = gridGfs.findOne(query);
File outFile = new File("/Users/Khalid/git/gists/file.zip");
outFile.createNewFile();
zipFile.writeTo(outFile);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment