MongoDB GridFS

1.利用Mongofile命令进行文件的操作

usage: mongofiles.exe [options] command [gridfs filename]

command:

  one of (list|search|put|get)

  list - list all files.  'gridfs filename' is an optional prefix //查看所有文件

         which listed filenames must begin with.

  search - search all files. 'gridfs filename' is a substring //查找文件

           which listed filenames must contain.

  put - add a file with filename 'gridfs filename' //上传文件

  get - get a file with filename 'gridfs filename' //获取文件

  delete - delete all files with filename 'gridfs filename' //删除文件

options:

  --help                  produce help message

  -v [ --verbose ]        be more verbose (include multiple times for more

                          verbosity e.g. -vvvvv)

  -h [ --host ] arg       mongo host to connect to ( <set name>/s1,s2 for sets

  --port arg              server port. Can also use --host hostname:port

  --ipv6                  enable IPv6 support (disabled by default)

  -u [ --username ] arg   username

  -p [ --password ] arg   password

  --dbpath arg            directly access mongod database files in the given

                          path, instead of connecting to a mongod  server -

                          needs to lock the data directory, so cannot be used

                          if a mongod is currently accessing the same path

  --directoryperdb        if dbpath specified, each db is in a separate

                          directory

  -d [ --db ] arg         database to use

  -c [ --collection ] arg collection to use (some commands)

  -l [ --local ] arg      local filename for put|get (default is to use the

                          same name as 'gridfs filename')

  -t [ --type ] arg       MIME type for put (default is to omit)

  -r [ --replace ]        Remove other files with same name after PUT

 

2.内部原理

GridFS基本思想是可以将大文件分成很多块,每块作为一个单独的文档存储,这样就能存大文件了。由于MongoDB支持在文档中存储二进制数据,可以最大限度减小块的存储开销。另外,除了存储文件本身的块,还有一个单独的文档用来储存分块的信息和文件的元数据。

默认情况下使用fs.chunks集合存块。其文档结构如下:

{

"_id":ObjectId("......"),

"n":0,//表示块编号

"data":BinData("...."),//二进制数据

"files_id"//包含这个块元数据的文件文档的"_id"

}

默认使用fs.file存文件的元数据

> db.fs.files.find()

{ "_id" : ObjectId("4fc86cceddc24f04eed5b200"), "filename" : "../Alfresco.txt",

"chunkSize" : 262144, "uploadDate" : ISODate("2012-06-01T07:18:38.763Z"), "md5"

: "e3c2d56f6b6cf4ec09da825e24b6c758", "length" : 1021 }

>

其中_id,文件唯一的id,在块中作为files_id键的值存储

length,文件内容总的字节数

chunkSize每块的大小,以字节为单位,默认为256K,必要时可以调整。

uploadDate 文件存储的时间戳

md5 文件内容的md5的校验和,由服务器端生成。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值