MongoDB学习笔记29MongoDB的GridFS存储深入

GridFS的原理是将大文件分割为多个比较大的块,将每个块作为独立的文档进行存储。

(1)GridFS中的块会被存贮到专用的集合中,默认为fs.chunks;

(2)除了将文件的每一个块单独存储外,还需要将每个文件块的元信息放入一个文档存储,并将这些文档默认集中在fs.files集合中;

例如将foo.txt文档加入到数据库foo中:

[[email protected] ~]# echo "hello world" >foo.txt
[[email protected] ~]# mongofiles -d foo put foo.txt
connected to: 127.0.0.1
added file: { _id: ObjectId('54b3d62983047a88669bc529'), filename: "foo.txt", chunkSize: 261120, uploadDate: new Date(1421071914003), md5: "6f5902ac237024bdd0c176cb93063dc4", length: 12 }
done!

查看相应的集合:

> show collections
fs.chunks
fs.files
system.indexes
>

查看这个文档:

[[email protected] ~]# mongo foo
MongoDB shell version: 2.6.6
connecting to: foo
> db.fs.chunks.findOne()
{
"_id" : ObjectId("54b51dc15caeb0e1a8722e8d"),
"files_id" : ObjectId("54b51dc0c3ab6ae7c08f6028"),
"n" : 0,
"data" : BinData(0,"aGVsbG8gd29ybGQK")
}
>

files_id:块所属文件的元信息;

n: 块在文件中的相对位置;

data:块所包含的二进制数据;

> db.fs.files.findOne()
{
"_id" : ObjectId("54b51dc0c3ab6ae7c08f6028"),
"filename" : "foo.txt",
"chunkSize" : 261120,
"uploadDate" : ISODate("2015-01-13T13:29:37.525Z"),
"md5" : "6f5902ac237024bdd0c176cb93063dc4",
"length" : 12
}
>

_id:文件的唯一ID,跟文件的每个块中的files_id的值是一致的;

length:文件所包含的字节数;

chunkSize:组成文件的每个块的大小,单位是字节;

updateDate:文件被上传到GridFS的时间;

MD5:文件的MD5值,由服务器计算所得;

本文出自 “缘随心愿” 博客,请务必保留此出处http://281816327.blog.51cto.com/907015/1603698

Database error: [Table 'ac_search_cache' is marked as crashed and should be repaired]

SELECT * FROM ac_search_cache WHERE hash = '099ee1dd7381ec28005751067308d06df98b26cd' LIMIT 1;