MongoDB Study notes (2)

Source: Internet
Author: User
Tags findone mongoclient mongodb
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.