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:
- _ID: The file's unique ID, stored in the block for the value of "files_id" key
- Length: The total number of bytes in the file contents
- Chunksize: The size of each block, in bytes. Default 25K, can be adjusted
- Uploaddate: The time the file was deposited
- 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 }
- _ID: Logo Unique
- N: Indicates the number of the block, which is the ordinal number of the block in the original file.
- Data: Contains binary data that makes up a block of files
- 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