Gridfs as a storage mechanism for MongoDB, used to store binary large files;
Gridfs has the following advantages:
(1) Simplify the storage stack with GRIDFS and replace the Independent storage tool in MongoDB;
(2) Gridfs will automatically balance the existing replication and automatic shards, it is easier to do a failover of file storage to swing horizontally;
(3) MongoDB in the size of 2GB to allocate data files, in the Gridfs file storage concentration will be relatively high;
Gridfs, of course, also has drawbacks:
(1) Read files from Gridfs is not directly from the file system fast;
(2) Modify the document stored in the Gridfs, only delete the document and then save the document from the new one;
(3) If the large file is stored as multiple files, it is not possible to lock all the file blocks when modifying this large document;
For the above advantages and disadvantages, it can be seen that GRIDFS is more suitable for storing infrequently modified large files.
Manage Gridefs with Mongofiles
(1) Use-help to view mongofiles parameters
[Email protected] ~]# mongofiles--help Browse and modify a GRIDFS filesystem.
(2) Upload a file to Gridfs
[Email protected] ~]# echo "Hello World" >foo.txt [[email protected] ~]# mongofiles 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!
(3) List the documents stored in the Gridfs
[Email protected] ~]# mongofiles list connected to:127.0.0.1 foo.txt 12
(4) Download the Gridfs in the document to the file system
[[Email protected] ~]# RM foo.txt rm:remove regular file ' Foo.txt '? y [[email protected] ~]# mongofiles get foo.txt connected to:127.0.0.1 done Write to:foo.txt [[email Protect Ed] ~]# Cat Foo.txt Hello World
(5) Search and delete documents
[Email protected] ~]# Mongofiles search foo.txt connected to:127.0.0.1 foo.txt [[email protected] ~]# Mongofi Les delete foo.txt connected to:127.0.0.1 done! [Email protected] ~]# Mongofiles search foo.txt connected to:127.0.0.1 [[email protected] ~]#
This article is from the "Margin with Wish" blog, please be sure to keep this source http://281816327.blog.51cto.com/907015/1602645
"MongoDB Learning note 28" MongoDB's Gridfs storage mechanism