Docs HomeMongoDB Manual

GridFS

GridFS is a specification for storing and retrieving files that exceed the BSON-document size limit of 16 MB.是用于存储和检索超过BSON文档大小限制16 MB的文件的规范。

Note

GridFS does not support multi-document transactions.GridFS不支持多文档事务

Instead of storing a file in a single document, GridFS divides the file into parts, or chunks [1], and stores each chunk as a separate document. By default, GridFS uses a default chunk size of 255 kB; that is, GridFS divides a file into chunks of 255 kB with the exception of the last chunk. GridFS不是将文件存储在单个文档中,而是将文件划分为多个部分或块[1],并将每个块存储为一个单独的文档。默认情况下,GridFS使用255 kB的默认块大小;也就是说,GridFS将文件划分为255 kB的块,最后一个块除外。The last chunk is only as large as necessary. Similarly, files that are no larger than the chunk size only have a final chunk, using only as much space as needed plus some additional metadata.最后一个区块只有在必要时才大。类似地,不大于块大小的文件只有一个最终块,只使用所需的空间和一些额外的元数据。

GridFS uses two collections to store files. One collection stores the file chunks, and the other stores file metadata. The section GridFS Collections describes each collection in detail.GridFS使用两个集合来存储文件。一个集合存储文件块,另一个存储文件元数据。GridFS集合一节详细描述了每个集合。

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. 当您在GridFS中查询文件时,驱动程序将根据需要重新组装块。您可以对通过GridFS存储的文件执行范围查询。You can also access information from arbitrary sections of files, such as to "skip" to the middle of a video or audio file.您还可以访问文件任意部分的信息,例如“跳过”到视频或音频文件的中间。

GridFS is useful not only for storing files that exceed 16 MB but also for storing any files for which you want access without having to load the entire file into memory. GridFS不仅适用于存储超过16MB的文件,还适用于存储您想要访问的任何文件,而无需将整个文件加载到内存中。See also When to Use GridFS.另请参阅何时使用GridFS

When to Use GridFS何时使用GridFS

In MongoDB, use GridFS for storing files larger than 16 MB.在MongoDB中,使用GridFS存储大于16MB的文件。

In some situations, storing large files may be more efficient in a MongoDB database than on a system-level filesystem.在某些情况下,在MongoDB数据库中存储大文件可能比在系统级文件系统中更高效。

  • If your filesystem limits the number of files in a directory, you can use GridFS to store as many files as needed.如果文件系统限制了目录中的文件数量,则可以使用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.当您想访问大文件的部分信息而不必将整个文件加载到内存中时,可以使用GridFS调用文件的部分,而无需将整个文件读取到内存中。
  • When you want to keep your files and metadata automatically synced and deployed across a number of systems and facilities, you can use GridFS. 当您希望在多个系统和设施中自动同步和部署文件和元数据时,可以使用GridFS。When using geographically distributed replica sets, MongoDB can distribute files and their metadata automatically to a number of mongod instances and facilities.当使用地理分布的副本集时,MongoDB可以自动将文件及其元数据分发到许多mongod实例和设施。

Do not use GridFS if you need to update the content of the entire file atomically. 如果需要以原子方式更新整个文件的内容,请不要使用GridFS。As an alternative you can store multiple versions of each file and specify the current version of the file in the metadata. 或者,您可以存储每个文件的多个版本,并在元数据中指定文件的当前版本。You can update the metadata field that indicates "latest" status in an atomic update after uploading the new version of the file, and later remove previous versions if needed.您可以在上传文件的新版本后更新原子更新中指示“最新”状态的元数据字段,并在以后根据需要删除以前的版本。

Furthermore, if your files are all smaller than the 16 MB BSON Document Size limit, consider storing each file in a single document instead of using GridFS. 此外,如果您的文件都小于16 MB BSON文档大小限制,请考虑将每个文件存储在单个文档中,而不是使用GridFS。You may use the BinData data type to store the binary data. See your drivers documentation for details on using BinData.您可以使用BinData数据类型来存储二进制数据。有关使用BinData的详细信息,请参阅驱动程序文档。

Use GridFS使用GridFS

To store and retrieve files using GridFS, use either of the following:要使用GridFS存储和检索文件,请使用以下任一选项:

  • A MongoDB driver. See the drivers documentation for information on using GridFS with your driver.MongoDB驱动程序。有关在驱动程序中使用GridFS的信息,请参阅驱动程序文档。
  • The mongofiles command-line tool. See the mongofiles reference for documentation.mongofiles命令行工具。有关文档,请参阅mongofiles参考。

GridFS CollectionsGridFS集合

GridFS stores files in two collections:将文件存储在两个集合中:

GridFS places the collections in a common bucket by prefixing each with the bucket name. GridFS通过在每个集合前面加上桶名称,将集合放在一个公共桶中。By default, GridFS uses two collections with a bucket named fs:默认情况下,GridFS使用两个具有名为fs的桶的集合:

  • fs.files
  • fs.chunks

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.包含桶名称的完整集合名称受命名空间长度限制

The chunks Collectionchunks集合

Each document in the chunks [1] collection represents a distinct chunk of a file as represented in GridFS. Documents in this collection have the following form:chunks [1]集合中的每个文档表示GridFS中表示的文件的一个不同的块。此集合中的文档具有以下形式:

{
"_id" : <ObjectId>,
"files_id" : <ObjectId>,
"n" : <num>,
"data" : <binary>
}

A document from the chunks collection contains the following fields:chunks集合中的文档包含以下字段:

chunks._id

The unique ObjectId of the chunk.区块的唯一ObjectId

chunks.files_id

The _id of the "parent" document, as specified in the files collection.files集合中指定的“父”文档的_id

chunks.n

The sequence number of the chunk. GridFS numbers all chunks, starting with 0.区块的序列号。GridFS对所有区块进行编号,从0开始。

chunks.data

The chunk's payload as a BSON Binary type.区块的有效负载为BSON二进制类型。

The files Collectionfiles集合

Each document in the files collection represents a file in GridFS.files集合中的每个文档都表示GridFS中的一个文件。

{
"_id" : <ObjectId>,
"length" : <num>,
"chunkSize" : <num>,
"uploadDate" : <timestamp>,
"md5" : <hash>,
"filename" : <string>,
"contentType" : <string>,
"aliases" : <string array>,
"metadata" : <any>,
}

Documents in the files collection contain some or all of the following fields:files集合中的文档包含以下部分或全部字段:

files._id

The unique identifier for this document. The _id is of the data type you chose for the original document. 此文档的唯一标识符。_id是您为原始文档选择的数据类型。The default type for MongoDB documents is BSON ObjectId.MongoDB文档的默认类型是BSON ObjectId

files.length

The size of the document in bytes.文档的大小(以字节为单位)。

files.chunkSize

The size of each chunk in bytes. 每个区块的大小(以字节为单位)。GridFS divides the document into chunks of size chunkSize, except for the last, which is only as large as needed. The default size is 255 kilobytes (kB).GridFS将文档分为chunkSize大小的块,除了最后一个块,后者只根据需要而大。默认大小为255千字节(kB)。

files.uploadDate

The date the document was first stored by GridFS. This value has the Date type.GridFS首次存储文档的日期。此值具有Date类型。

files.md5

Deprecated

The MD5 algorithm is prohibited by FIPS 140-2. MongoDB drivers deprecate MD5 support and will remove MD5 generation in future releases. MD5算法被FIPS 140-2禁止。MongoDB驱动程序反对MD5支持,并将在未来的版本中删除MD5生成。Applications that require a file digest should implement it outside of GridFS and store in files.metadata.需要文件摘要的应用程序应该在GridFS之外实现它,并存储在files.metadata中。

An MD5 hash of the complete file returned by the filemd5 command. This value has the String type.filemd5命令返回的完整文件的MD5哈希。此值具有String类型。

files.filename

Optional. A human-readable name for the GridFS file.可选择的GridFS文件的可读名称。

files.contentType

Deprecated

Optional. A valid MIME type for the GridFS file. For application use only.可选择的GridFS文件的有效MIME类型。仅供应用程序使用。

Use files.metadata for storing information related to the MIME type of the GridFS file.使用files.metadata存储与GridFS文件的MIME类型相关的信息。

files.aliases

Deprecated

Optional. An array of alias strings. For application use only.可选择的别名字符串的数组。仅供应用程序使用。

Use files.metadata for storing information related to the MIME type of the GridFS file.使用files.metadata存储与GridFS文件的MIME类型相关的信息。

files.metadata

Optional. The metadata field may be of any data type and can hold any additional information you want to store. If you wish to add additional arbitrary fields to documents in the files collection, add them to an object in the metadata field.可选择的元数据字段可以是任何数据类型,并且可以包含要存储的任何附加信息。如果要向文件集合中的文档添加其他任意字段,请将它们添加到元数据字段中的对象中。

GridFS IndexesGridFS索引

GridFS uses indexes on each of the chunks and files collections for efficiency. 为了提高效率,GridFS在每个chunksfiles集合上使用索引。Drivers that conform to the GridFS specification automatically create these indexes for convenience. 为了方便起见,符合GridFS规范驱动程序会自动创建这些索引。You can also create any additional indexes as desired to suit your application's needs.您还可以根据需要创建任何其他索引,以满足应用程序的需要。

The chunks Indexchunks索引

GridFS uses a unique, compound index on the chunks collection using the files_id and n fields. GridFS使用files_idn字段对chunks集合使用唯一的复合索引。This allows for efficient retrieval of chunks, as demonstrated in the following example:这允许有效地检索组块,如以下示例所示:

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

Drivers that conform to the GridFS specification will automatically ensure that this index exists before read and write operations. 符合GridFS规范驱动程序将自动确保此索引在读写操作之前存在。See the relevant driver documentation for the specific behavior of your GridFS application.有关GridFS应用程序的具体行为,请参阅相关的驱动程序文档。

If this index does not exist, you can issue the following operation to create it using mongosh:如果此索引不存在,您可以发出以下操作来使用mongosh创建它:

db.fs.chunks.createIndex( { files_id: 1, n: 1 }, { unique: true } );

The files Indexfiles索引

GridFS uses an index on the files collection using the filename and uploadDate fields. 使用filenameuploadDate字段对files集合使用索引。This index allows for efficient retrieval of files, as shown in this example:此索引允许高效检索文件,如本例所示:

db.fs.files.find( { filename: myFileName } ).sort( { uploadDate: 1 } )

Drivers that conform to the GridFS specification will automatically ensure that this index exists before read and write operations. See the relevant driver documentation for the specific behavior of your GridFS application.符合GridFS规范驱动程序将自动确保此索引在读写操作之前存在。有关GridFS应用程序的具体行为,请参阅相关的驱动程序文档。

If this index does not exist, you can issue the following operation to create it using mongosh:如果此索引不存在,您可以发出以下操作来使用mongosh创建它:

db.fs.files.createIndex( { filename: 1, uploadDate: 1 } );
[1](1, 2) The use of the term chunks in the context of GridFS is not related to the use of the term chunks in the context of sharding.在GridFS上下文中使用术语与在分片上下文中使用词无关。

Sharding GridFS分片 GridFS

There are two collections to consider with GridFS - files and chunks.GridFS有两个集合需要考虑——fileschunks

chunks Collection集合

To shard the chunks collection, use either { files_id : 1, n : 1 } or { files_id : 1 } as the shard key index. 要对chunks集合进行分片,请使用{ files_id : 1, n : 1 }{ files_id : 1 }作为分片键索引。files_id is an ObjectId and changes monotonically.files_id是一个ObjectId,并且单调变化。

For MongoDB drivers that do not run filemd5 to verify successful upload (for example, MongoDB drivers that support MongoDB 4.0 or greater), you can use Hashed Sharding for the chunks collection.对于不运行filemd5来验证上传成功的MongoDB驱动程序(例如,支持MongoDB 4.0或更高版本的MongoDB驱动器),您可以使用哈希分片来集合区块。

If the MongoDB driver runs filemd5, you cannot use Hashed Sharding. 如果MongoDB驱动程序运行filemd5,则不能使用哈希分片For details, see SERVER-9888.

files Collection集合

The files collection is small and only contains metadata. files集合很小,仅包含元数据。None of the required keys for GridFS lend themselves to an even distribution in a sharded environment. GridFS所需的键都不适用于分片环境中的均匀分布。Leaving files unsharded allows all the file metadata documents to live on the primary shard.files保持为无记录状态允许所有文件元数据文档都位于主分片上

If you must shard the files collection, use the _id field, possibly in combination with an application field.如果必须files集合进行分片,请使用_id字段,可能与应用程序字段结合使用。