数据库

 首页 > 数据库 > MongoDB > MongoDB与GridFS文件系统

MongoDB与GridFS文件系统

分享到:
【字体:
导读:
         摘要:GridFS用于存储和恢复那些超过16M(BSON文件限制)的文件。GridFS将文件分成大块,将每个大块存储为单独的文件.GridFS中限制chunk最大为256k。GridFS使用两个collection存储,一个存储chunks,一个存储元数据(metadata)。fs.files和fs.chunksWhensh...

MongoDB与GridFS文件系统


GridFS用于存储和恢复那些超过16M(BSON文件限制)的文件。

GridFS将文件分成大块,将每个大块存储为单独的文件.GridFS中限制chunk最大为256k。GridFS使用两个collection存储,一个存储chunks,一个存储元数据(metadata)。
fs.files和fs.chunks


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

 


file Collection:具体形式如下
{
  "_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:举例如下
{
  "_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使用chunks中files_id和n域作为混合索引,files_id是父文档的_id,n域包含chunk的序列号,该值从0开始。
GridFS索引支持快速恢复数据。

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

如果没有建立索引,可以使用下列shell命令:
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"));

接口支持额外的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"));

MongoDB与GridFS文件系统
分享到:
MongoDB与GridFS文件系统的使用方法
MongoDB与GridFS文件系统的使用方法     GridFS 是一种将大型文件存储在 MongoDB 数据库中的文件规范。所有官方支持的驱动均实现了 GridFS 规范。 1 为什么要用 GridFS     由于 MongoDB 中 BSON 对象大小是有限制的,所以 GridFS 规范提供了一种透明的机制,可 以将一个大文件分割成为多个较小的文档, 这样的机制...
MongoDB如何使用GridFS方式保存文件
MongoDB如何使用GridFS方式保存文件 根据官网介绍,BSON objects in MongoDB are limited to 4MB in size.   http://www.mongodb.org/display/DOCS/GridFS   因此重新写了那个操作类,使用GridFS来保存文件,代码很简单,但开始接触弄了比较长时间,有一个问题一直解决不了,我希望自己生成一个Guid的 _id 而不是...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……