Mongodb_gridfs_ Storing files

Source: Internet
Author: User
Tags unique id

Gridfs

MongoDB can save files in addition to saving various documents (JOSN structure). The GRIDFS specification provides a transparent mechanism for splitting a large file into smaller documents, which allows us to effectively save large file objects, especially for huge files such as video, high-definition images, and more.

Gridfs uses two tables to store data:

    • Files: Containing metadata objects

The metadata for the file is placed inside this collection, and the default fs.files. Each of these documents represents a file in Gridfs, and the custom metadata associated with the file is also stored in it, in addition to the user-defined key, GRIDFS has some default keys:

    1. _ID: The file's unique ID, stored in the block for the value of "files_id" key
    2. Length: The total number of bytes in the file contents
    3. Chunksize: The size of each block, in bytes. Default 25K, can be adjusted
    4. Uploaddate: The time the file was deposited
    5. MD5: MD5 checksum of file contents, generated by the server
    • Chunks: binary block with some other relevant information

The GRIDFS block has a separate collection. By default, blocks will use the Fs.chunks collection and can be overwritten if necessary. The structure of the document in this block collection is very simple:

1 {  2 "_id": ObjectId ("..."  ),3 "n": 0,  4 "Data" : Bindata ("..."  ),5 "files_id": ObjectId ("...")  6 }  
    1. _ID: Logo Unique
    2. N: Indicates the number of the block, which is the ordinal number of the block in the original file.
    3. Data: Contains binary data that makes up a block of files
    4. files_id: "_id" of the file document containing this block metadata

Use Mongofile to manipulate Gridfs:

1   //write "AAAA" to the Foo.txt file2C:\mongodb\mongodb_win32_x86_64_2.2.7\bin>echo "AAAA" >foo.txt3  //save Foo.txt to Gridfs4C:\mongodb\mongodb_win32_x86_64_2.2.7\bin>Mongofiles.exe put Foo.txt5Connected to:127.0.0.16Added file: {_id:objectid (' 53449f0becdebde54ff76105 '), FileName: "Foo.txt", ch7unksize:262144, Uploaddate:NewDate (1397006091696), MD5: "819596956779801cced48fb21379bf023 ", Length:9 }  9done!Ten //View OneC:\mongodb\mongodb_win32_x86_64_2.2.7\bin>Mongofiles.exe List AConnected to:127.0.0.1 -Foo.txt 9 - //Get theC:\mongodb\mongodb_win32_x86_64_2.2.7\bin>Mongofiles.exe Get Foo.txt -Connected to:127.0.0.1 -Done Write To:foo.txt

Command description

Put: Add a file from the file system to the Gridfs

List: Lists all the files added to the Gridfs

Reverse operation of the Get:put

View Gridfs in the MongoDB tool

Mongodb_gridfs_ Storing files

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.