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" : <ObjectID>,
  "length" : <num>,
  "chunkSize" : <num>
  "uploadDate" : <timestamp>
  "md5" : <hash>

"filename" : <string>,
  "contentType" : <string>,
  "aliases" : <string array>,
  "metadata" : <dataObject>,
}

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" : <string>,
  "files_id" : <string>,
  "n" : <num>,
  "data" : <binary>
}

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的更多相关文章

  1. CentOS6.3搭建Nginx代理访问MongoDB GridFS图片资源

    PHP可以直接读取MongoDB GridFS中的图片并显示到页面中,但对PHP的压力就大了.偶然机会,了解到Nginx可以代理访问,实现过程如下: 1.工具准备 安装一些必要的编译工具及库,这里是直 ...

  2. MongoDB的学习和使用(MongoDB GridFS)

    MongoDB GridFS GridFS 用于存储和恢复那些超过16M(BSON文件限制)的文件(如:图片.音频.视频等). GridFS 也是文件存储的一种方式,但是它是存储在MonoDB的集合中 ...

  3. MongoDB GridFS 存储大文件

    我们经常会遇到这样的场景:上传/下载文件. 有两种思路可以解决这个问题: (1)将文件存储在服务器的文件系统中: (2)将文件存储在数据库中. 如果我们选择(2),那么我们可以使用MongoDB Gr ...

  4. MongoDB GridFS 存储文件

    使用MongoDB的GridFS方式. CSDN: https://blog.csdn.net/qq_32657967/article/details/81534259官方文档: https://do ...

  5. MongoDb GridFS的使用

    MongoDb GridFS 是MongoDB的文件存储方案,主要用于存储和恢复那些超过16M(BSON文件限制)的文件(如:图片.音频等),对大文件有着更好的性能. 要在C#中使用GridFS,首先 ...

  6. mongodb gridfs基本使用

    Mongodb GridFS图片文件存储解决方案 之前解决方案是接收图片数据后,将图片直接存储到盘阵,然后通过Apache做服务器,将图片信息存储到数据库,并且存储一个Apache的访问路径. 目前需 ...

  7. MongoDB GridFS最佳应用概述

    <MongoDB GridFS最佳应用概述> 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs GridFS是MongoDB数据库之上的一个简单 ...

  8. MongoDB GridFS——本质上是将一个文件分割为大小为256KB的chunks 每个chunk里会放md5标识 取文件的时候会将这些chunks合并为一个整体返回

    MongoDB GridFS GridFS 用于存储和恢复那些超过16M(BSON文件限制)的文件(如:图片.音频.视频等). GridFS 也是文件存储的一种方式,但是它是存储在MonoDB的集合中 ...

  9. Spring Boot使用MongoDB GridFS进行文件的操作

    1. GridFS简介 GridFS 用于存储和恢复那些超过16M(BSON文件限制)的文件(如:图片.音频.视频等),但是它是存储在MonoDB的集合中. GridFS 会将文件对象分割成多个的ch ...

  10. 【荐】PHP操作MongoDB GridFS 存储文件,如图片文件

    GridFS是MongoDB的一个内置功能,它提供一组文件操作的API以利用MongoDB存储文件,GridFS的基本原理是将文件保存在两个Collection中,一个保存文件索引,一个保存文件内容, ...

随机推荐

  1. linux配置网卡

    我爱折腾.在本地虚拟机里装了linux的环境.要配置linux的网卡文件. 如下: vi /etc/sysconfig/network-script/ifcfg-eth0; 刚装完系统,没有vim , ...

  2. ECMAScript严格模式简介

    写在前面 大家都知道使用"use strict"表示使用ECMAScript进行严格模式,使用"use strict"有两种方式 在文件头部写上它(使得整个脚本 ...

  3. chrome使用技巧

    chrome使用技巧 chrome对于开发人员来说,绝对是一个神器.下面,介绍关于它的一些小技巧: 1.利用chrome快速定位source中的资源. 我之前一般如果查找每个文件,都是打开控制台,在s ...

  4. 深入理解javascript中执行环境(作用域)与作用域链

    深入理解javascript中执行环境(作用域)与作用域链 相信很多初学者对与javascript中的执行环境与作用域链不能很好的理解,这里,我会按照自己的理解同大家一起分享. 一般情况下,我们把执行 ...

  5. tail 命令 查看Tomcat目录下日志的最后几行的方法

    工作中需要查看日志信息,进行排错,但是面对上万行的错误日志,从头开始往后看,比较浪费时间,所有使用tail命令会节省不少时间. 1.命令   tail  - n  opt/tomcat/logs/ca ...

  6. CentOS7+hadoop2.6.4+spark-1.6.1

    环境: CentOS7 hadoop2.6.4已安装两个节点:master.slave1 过程: 把下载的scala.spark压缩包拷贝到/usr/hadoop-2.6.4/thirdparty目录 ...

  7. 写Action的三种方法

    Action类似于servlet,在用户对浏览器输入url访问的时候充当控制器的角色.它在访问时产生,执行execute()之后就销毁了. 写Action是代理事务,它实现的三种方式是: (1)POJ ...

  8. bootstrap标签引入地址

    http://www.bootcdn.cn/bootstrap/ <link rel="stylesheet" href="http://apps.bdimg.co ...

  9. coreseek 中文搜索和高亮

    配置文件 # # Minimal Sphinx configuration sample (clean, simple, functional) # source post { type = mysq ...

  10. Python学习笔记——文件写入和读取

    1.文件写入 #coding:utf-8 #!/usr/bin/env python 'makeTextPyhton.py -- create text file' import os ls = os ...