12
MONGO GRIDFS

Mongo gridfs

Embed Size (px)

Citation preview

Page 1: Mongo gridfs

MONGO GRIDFS

Page 2: Mongo gridfs

GRIDFS

• GridFS is a specification for storing and retrieving files that exceed the BSON-document size limit of 16 MB

• Instead of storing a file in a single document, GridFS divides the file into parts, or chunks , and stores each chunk as a separate document.

• By default, GridFS uses a chunk size of 255 kB; that is, GridFS divides a file into chunks of 255 kB with the exception of the last chunk.

Page 3: Mongo gridfs
Page 4: Mongo gridfs

WHEN TO USE GRIDFS

• When you want to access information from portions of large files without having to load whole files into memory, you can use GridFS to recall sections of files without reading the entire file into memory.

• When you want to keep your files and metadata automatically synced and deployed across a number of systems and facilities, you can use GridFS. When using geographically distributed replica sets, MongoDB can distribute files and their metadata automatically to a number of mongod instances and facilities.

• If your filesystem limits the number of files in a directory, you can use GridFS to store as many files as needed.

Page 5: Mongo gridfs

WHEN NOT TO USE GRIDFS

• Do not use GridFS if you need to update the content of the entire file atomically. As an alternative you can store multiple versions of each file and specify the current version of the file in the metadata.

• If your files are all smaller the 16 MB BSON Document Size limit, consider storing the file manually within a single document instead of using GridFS

Page 6: Mongo gridfs

HOW IT WORKS

• GridFS uses two collections to store files. One collection stores the file chunks, and the other stores file metadata.• chunks stores the binary chunks.

• files stores the file’s metadata.

• When you query GridFS for a file, the driver will reassemble the chunks as needed. You can perform range queries on files stored through GridFS. You can also access information from arbitrary sections of files, such as to “skip” to the middle of a video or audio file.

Page 7: Mongo gridfs

SAMPLE DOCUMENT IN FILES COLLECTION{

        "_id" : ObjectId("5757f089187a62385c604560"),        "length" : NumberLong(21063225),        "chunkSize" : 261120,        "uploadDate" : ISODate("2016-06-08T10:16:43.562Z"),        "md5" : "54a81d15304172aa74b7fd780ed25528",        "filename" : "Kathy.mp4",        "metadata" : {                "cust_first_name" : "Kathy",                "CustID" : "51027",                "campgnName" : "input",                "TCS_REF" : "1027",                "videoName" : "Kathy.mp4"        }}

Page 8: Mongo gridfs

SAMPLE DOCUMENT IN CHUNKS COLLECTION

"_id" : ObjectId("5757f087187a62385c604469"), 

"files_id" : ObjectId("5757f087187a62385c604467"), 

"n" : 0, 

"data" : BinData(0,"AAAAIGZ0eXBpc29tAAACAGlzb21pc28y)

}

Page 9: Mongo gridfs

NAMESPACES

You can choose a different bucket name, as well as create multiple buckets in a single database. The full collection name, which includes the bucket name, is subject to the namespace length limit.

Page 10: Mongo gridfs

GRIDFS INDEXES

• GridFS uses indexes on each of the chunks and files collections for efficiency.• GridFS uses a unique, compound index on the chunks collection using the files_id and n fields.

• GridFS uses an index on the files collection using the filename and uploadDate fields.

• These are created automatically by default. 

• Additional indexes can be created if required.

Page 11: Mongo gridfs

FRAGMENTATION

• Stores in continuous memory locations for faster retrieval.

• Even if it is fragmented while insertion, Mongo DB compression process will defragment the files.

Page 12: Mongo gridfs

DRIVERS

• Java Driver examples

• https://api.mongodb.com/java/3.2/com/mongodb/client/gridfs/GridFSBucket.html