Get the App
SLTechnology News&Howtos  ›  Database  › 

[MongoDB Learning Notes 29] GridFS storage depth of MongoDB

Shulou Source: shulou.com Published: 2022-06-01 09:29:35 09月17日 Update

The principle of GridFS is to divide a large file into several larger blocks and store each block as a separate document.

(1) blocks in GridFS are stored in a dedicated collection. Default is fs.chunks.

(2) in addition to storing each block of a file separately, you also need to put the meta-information of each block into a document store and set these documents in the fs.files collection by default.

For example, add an foo.txt document to the database foo:

[root@localhost] # echo "hello world" > foo.txt [root@localhost] # mongofiles-d foo put foo.txt connected to: 127.0.0.1 added file: {_ id: ObjectId ('54b3d62983047a88669bc529'), filename: "foo.txt", chunkSize: 261120, uploadDate: new Date (1421071914003), md5: "6f5902ac237024bdd0c176cb93063dc4", length: 12} done!

View the appropriate collection:

> show collections fs.chunks fs.files system.indexes >

View this document:

[root@localhost ~] # mongo foo MongoDB shell version: 2.6.6 connecting to: foo > db.fs.chunks.findOne () {"_ id": ObjectId ("54b51dc15caeb0e1a8722e8d"), "files_id": ObjectId ("54b51dc0c3ab6ae7c08f6028"), "n": 0, "data": BinData (0, "aGVsbG8gd29ybGQK")} >

Files_id: meta information of the file to which the block belongs

N: relative position of the block in the file

Data: binary data contained in a block

> db.fs.files.findOne () {"_ id": ObjectId ("54b51dc0c3ab6ae7c08f6028"), "filename": "foo.txt", "chunkSize": 261120, "uploadDate": ISODate ("2015-01-13T13:29:37.525Z"), "md5": "6f5902ac237024bdd0c176cb93063dc4", "length": 12} >

_ id: the unique ID of the file, which is consistent with the value of files_id in each block of the file

Length: the number of bytes contained in the file

ChunkSize: the size of each block that makes up the file, in bytes

UpdateDate: the time when the file was uploaded to GridFS

MD5: MD5 value of the file, calculated by the server

Tags: File document storage information byte data consistent binary location unit principle multiple size ownership income database time server and will dedicated Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Technology Microsoft OPPO Reno vpn