MongoDB Gridfs File System
Practical Exercises
1. View the mongofile help document with the –help option
2. Put {FileName} through mongofile-h {Ip:port}-d {CollectionName}
where the-h option specifies the host name and the port number, the-d option specifies the database name, and the put action adds the file to the MongoDB.
3.
Gridfs in the database, fs.chunks and fs.files are used by default to store files.
Where the Fs.files collection holds the file information, Fs.chunks holds the file data
4.
5.
6.
list: Getting file lists
get: Getting files
Delete: deleting files
Search: Query file
7. Through the Java driver Operation MongoDB
"User realization" MongoDB Gridfs file system
public class gridfsdemon{public static void Main (string[] args) {//TODO auto-generated method stub Str
ing URL = "192.168.136.138:27017";
Mongoclient mongoclient = new Mongoclient (URL);
DB db = Mongoclient.getdb ("Jike");
Gridfs Gridfs = new Gridfs (db);
File File = new file (System.getproperty ("user.home") + "/mongodb-manaul.pdf");
try {//save files FileInputStream fileinputstream = new FileInputStream (file);
Gridfsinputfile CreateFile = Gridfs.createfile (FileInputStream);
Createfile.put ("FileName", "mongodb_manual_3.0_2.pdf");
Createfile.put ("ContentType", "application/pdf");
Createfile.save ();
Fetch file Gridfsdbfile FindOne = Gridfs.findone (New Basicdbobject ("_id", Createfile.getid ());
System.out.println (FindOne);
Take the file list dbcursor filelist = Gridfs.getfilelist ();
while (Filelist.hasnext ()) {System.out.println (Filelist.next ());
///delete file Gridfs.remove (new Basicdbobject ("_id", Createfile.getid ());
catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();
finally {mongoclient.close (); }
}
}
The
is visible from the previous illustration and successfully uploaded to MongoDB via the Java driver file.