1024programmer Mongodb MongoDB and GridFS file system

MongoDB and GridFS file system


GridFS is used to store and restore files that exceed 16M (BSON file limit).

GridFS divides files into large blocks and stores each large block as a separate file. The maximum chunk limit in GridFS is 256k. GridFS uses two collection stores, one for chunks and one for metadata.
fs.files and fs.chunks


When should I use GridFS?
http://docs.mongodb.org/manual/faq/developers/#faq-developers-when-to-use-gridfs

the


file Collection: the specific form is as follows
{
“_id” : ,
“length” : ,
“chunkSize” :
“uploadDate” :
“md5” :

“filename”: ,
“contentType” : ,
“aliases” : ,
“metadata” : ,
}

Documents in the files collection contain some or all of the
following fields. Applications may create additional arbitrary
fields:

files._id
The unique ID for this document. The _id is of
the data type you chose for the original document. The default type
for MongoDB documents is BSON ObjectID.

files. length
The size of the document in bytes.

files.chunkSize
The size of each chunk. GridFS divides the
document into chunks of the size specified here. The default size
is 256 kilobytes.

files.uploadDate
The date the document was first stored by
GridFS. This value has the Date type.

files.md5
An MD5 hash returned from the filemd5 API. This
value has the String type.

files.filename
  Optional. A human-readable name for the
document.

files. contentType
  Optional. A valid MIME type for the
document.

files.aliases
Optional. An array of alias strings.

files.metadata
Optional. Any additional information you want to
store.

The chunks Collection: Examples are as follows
{
“_id” : ,
“files_id” : ,
“n” : ,
“data” :
}

A document from the chunks collection contains the following
fields:
chunks._id
The unique ObjectID of the chunk.

chunks.files_id
The _id of the “parent” document, as specified
in the files collection.

chunks.n
The sequence number of the chunk. GridFS numbers
all chunks, starting with 0.

chunks.data
  The chunk’s payload as a BSON binary type.

GridFS Index

GridFS uses the files_id and n fields in chunks as a mixed index. files_id is the _id of the parent document, and the n field contains the serial number of the chunk, which starts from 0.
GridFS indexes support fast data recovery.

cursor = db.fs.chunks.find({files_id:
myFileID}).sort({n:1});

If not indexed, the following shell command can be used:
db.fs.chunks.ensureIndex( { files_id: 1, n: 1 }, { unique: true }
);

Example Interface:

// returns default GridFS bucket (i.e. “fs” collection)
GridFS myFS = new GridFS(myDatabase);

// saves the file to “fs” GridFS bucket
myFS.createFile(new File(“/tmp/largething.mpg”));

Interface supports additional GridFS buckets
// returns GridFS bucket named “contracts”
GridFS myCOntracts= new GridFS(myDatabase, “contracts”);

// retrieve GridFS object “smithco”
GridFSDBFile file = myContracts. findOne(“smithco”);

// saves the GridFS file to the file system
file.writeTo(new File(“/tmp/smithco.pdf”));

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/mongodb-and-gridfs-file-system/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索