mne*_*syn 19
GridFS通过为每个文件存储多个块来工作.这样,您可以传送和存储非常大的文件,而无需将整个文件存储在RAM中.此外,这使您可以存储大于最大文档大小的文件.建议的块大小为256kb.
文件元数据字段可用于存储其他特定于文件的元数据,这比将元数据存储在单独的文档中更有效.这在很大程度上取决于您的确切要求,但通常,元数据字段提供了很大的灵活性.请记住,fs.files默认情况下,一些更明显的元数据已经是文档的一部分:
> db.fs.files.findOne();
{
"_id" : ObjectId("4f9d4172b2ceac15506445e1"),
"filename" : "2e117dc7f5ba434c90be29c767426c29",
"length" : 486912,
"chunkSize" : 262144,
"uploadDate" : ISODate("2011-10-18T09:05:54.851Z"),
"md5" : "4f31970165766913fdece5417f7fa4a8",
"contentType" : "application/pdf"
}
Run Code Online (Sandbox Code Playgroud)
要从GridFS实际读取文件,您必须从中获取文件文档fs.files和从中获取文件块fs.chunks.最有效的方法是将其流式传输到客户端块,因此您不必将整个文件加载到RAM中.该chunks集合具有以下结构:
> db.fs.chunks.findOne({}, {"data" :0});
{
"_id" : ObjectId("4e9d4172b2ceac15506445e1"),
"files_id" : ObjectId("4f9d4172b2ceac15506445e1"),
"n" : 0, // this is the 0th chunk of the file
"data" : /* loads of data */
}
Run Code Online (Sandbox Code Playgroud)
如果要使用查询metadata字段,fs.files请确保您理解点符号,例如
> db.fs.files.find({"metadata.OwnerId": new ObjectId("..."),
"metadata.ImageWidth" : 280});
Run Code Online (Sandbox Code Playgroud)
还要确保您的查询可以使用索引explain().
正如规范所说,您可以在元数据字段中存储您想要的任何内容.
以下是文件集合中的文档的外观:
必填字段
{
"_id" : <unspecified>, // unique ID for this file
"length" : data_number, // size of the file in bytes
"chunkSize" : data_number, // size of each of the chunks. Default is 256k
"uploadDate" : data_date, // date when object first stored
"md5" : data_string // result of running the "filemd5" command on this file's chunks
}
Run Code Online (Sandbox Code Playgroud)
可选字段
{
"filename" : data_string, // human name for the file
"contentType" : data_string, // valid mime type for the object
"aliases" : data_array of data_string, // optional array of alias strings
"metadata" : data_object, // anything the user wants to store
}
Run Code Online (Sandbox Code Playgroud)
因此,将您想要的任何内容存储在元数据中,并像在MongoDB中一样查询它:
db.fs.files.find({"metadata.some_info" : "sample"});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16285 次 |
| 最近记录: |