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的校验和,由服务器端生成。