Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

[MongoDB Learning Notes 28] GridFS storage mechanism of MongoDB

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

As the storage mechanism of MongoDB, GridFS is used to store large binary files.

GridFS has the following advantages:

(1) use GridFS to simplify the storage stack and replace independent storage tools in MongoDB

(2) GridFS automatically balances existing replication and automatic fragmentation, and it is easier to fail over file storage and scale out.

(3) data files are allocated according to the size of 2GB in MongoDB, and the degree of file storage concentration is higher in GridFS.

GridFS certainly has its drawbacks:

(1) Reading files from GridFS is not as fast as reading files directly from the file system.

(2) modify the document stored in GridFS, only delete the document first and then save the document again

(3) if a large file is stored as multiple files, all file blocks cannot be locked when the large document is modified.

In view of the above advantages and disadvantages, we can see that GridFS is more suitable for storing large files that are not often modified.

Use mongofiles to manage GrideFS

(1) use-help to view mongofiles parameters

[root@localhost] # mongofiles-- help Browse and modify a GridFS filesystem.

(2) upload a file to GridFS in database foo

[root@localhost] # echo "hello world" > foo.txt [root@localhost] # 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!

(3) list the documents stored in GridFS

[root@localhost] # mongofiles-d foo list connected to: 127.0.0.1 foo.txt 12

(4) download the GridFS from the document to the file system

[root@localhost ~] # rm foo.txt rm: remove regular file `foo.txt'? Y [root@localhost ~] # mongofiles-d foo get foo.txt connected to: 127.0.0.1 done write to: foo.txt [root@localhost ~] # cat foo.txt hello world

(5) search and delete documents

[root@localhost ~] # mongofiles-d foo search foo.txt connected to: 127.0.0.1 foo.txt 12 [root@localhost ~] # mongofiles-d foo delete foo.txt connected to: 127.0.0.1 done! [root@localhost ~] # mongofiles-d foo search foo.txt connected to: 127.0.0.1 [root@localhost ~] #

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report