MongoDB Learning: Gridfs

Source: Internet
Author: User
Tags unique id

Gridfs is a mechanism for storing a binary file in Mongodb. Gridfs is used to store and restore files that exceed the 16M(BSON file limit) ( e.g. pictures, audio, video, etc.) ).

There are several reasons for using Gridfs :

1 using Gridfs to simplify requirements

2 Gridfs will directly take advantage of the established replication or fragmentation mechanism, so it is easy to recover and extend for file storage

3 Gridfs can avoid some problems with the file system that is used to store user uploaded content. Gridfs will divide large file objects into smaller chunk ( file Fragments ), typically 256k/ , each chunk will be stored as a document of MongoDB chunks in the collection.

4 Gridfs does not produce disk fragmentation.

Here's how to use it:

Gridfs The most recent use of mongofiles command,mongofiles can be used to upload, download, display files in Gridfs.

(1) Create a new Test.txt Document

[Email protected]:/home/zhf# echo "Hello Mongod" > Test.txt

[Email protected]:/home/zhf# cat Test.txt

Hello Mongod

(2) through Mongofiles Upload

[Email protected]:/home/zhf# mongofiles put test.txt

2017-12-31t22:06:26.489+0800 Connected To:localhost

Added File:test.txt

(3) Show Files

[Email protected]:/home/zhf# mongofiles List

2017-12-31t22:06:41.308+0800 Connected To:localhost

Test.txt 13

(4) Delete the file on this side

[Email protected]:/home/zhf# rm test.txt

[Email protected]:/home/zhf# cat Test.txt

Cat:test.txt: no file or directory

(5) from Gridfs Download the file.

[Email protected]:/home/zhf# mongofiles Get test.txt

2017-12-31t22:07:43.762+0800 Connected To:localhost

Finished writing to Test.txt

[Email protected]:/home/zhf# cat Test,txt

Cat:test,txt: no file or directory

[Email protected]:/home/zhf# cat Test.txt

Hello Mongod

So Gridfs specifically how to store the file. The previous introduction to Gridfs is to solve the problem of single document cannot exceed 16M . Save the file by slicing it into a separate collection : Fs.chunks, and save a File index table:fs.files. In the previous example, we uploaded a test.txt file. We go into the database and see how it's stored.

The test Database is more in the database

> Show DBS

Admin 0.000GB

Local 0.000GB

Maple 0.000GB

Test 0.000GB

There are two sets of fs.chunks and fs.files under the test database .

> Use test

Switched to DB test

> Show Collections

Fs.chunks

Fs.files

The fields in the chunks are interpreted as follows:

_ID: Unique ID of the file

FILES_ID: The _id of the file document containing this block metadata

N: represents the block number. That is, the order number of the block in the original file. Here for 0 stands for only one block

Data: binary data that makes up a file block

> Db.fs.chunks.find ()

{"_id": ObjectId ("5a48eee22d33c14783a13d55"), "files_id": ObjectId ("5a48eee22d33c14783a13d54"), "n": 0, "Data": Binda TA (0, "agvsbg8gbw9uz29kcg==")}

The fields in files are explained as follows:

_ID: Unique ID in the file

Length: file Lengths

Chunksize: the size of each block. In bytes

Uploaddate: timestamp of file deposit Gridfs

MD5: MD5 checksum of the contents of the file . Generated by the server side.

> Db.fs.files.find ()

{"_id": ObjectId ("5a48eee22d33c14783a13d54"), "chunkSize": 261120, "uploaddate": Isodate ("2017-12-31t14:06:27.294z" ), "Length": +, "MD5": "40a1f080f26be5a4c8cef7d78f974659", "filename": "Test.txt"}

MongoDB Learning: Gridfs

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.