13. Gridfs of MongoDB

Refer to the official website as follows (the following links are progressive):

https://docs.mongodb.com/manual/core/gridfs/
https://docs.mongodb.com/database-tools/mongofiles/#mongodb-binary-bin.mongofiles
https://docs.mongodb.com/database-tools/installation/installation/

FS: the file system (File System).

Introduction to GridFS.

      Documents (data) in MongoDB are stored in BSON format. Due to the limitation of BSON, only data below 16M can be stored; in order to deal with this problem, Mongodb provides the GridFS submodule, which can store files larger than 16M. GridFS is a file specification for storing large files in MongoDB. All officially supported drivers implement the GridFS specification.

Note: In fact, if it is less than 16M, you can directly save it with MongoDB's bson, and if it is larger than 16M, you need to use Gridfs.

        GridFS uses two collections to store files: fs.files and fs.chunks. Among them, the fs.files collection stores the information of the files, and the fs.chunks stores the data of the files. GridFS will divide large file objects into multiple small chunks (file fragments), generally 256k/piece, and each chunk is stored in the chunks collection as a document of Mongodb.

mongofilesTools

        The mongofiles executable program provides command-line operations on files stored in MongoDB instances in GridFS objects, and it provides an interface between objects stored in the file system and GridFS. It is known from the official website that starting from MongoDB4.4, mongofiles is now released separately from the MongoDB server, and uses its own version control, the initial version is 100.0.0. Previously, mongofiles were distributed with the MongoDB server and used matching version control. For the installation of the tool, please refer to the official website.

The usage of mongofiles mainly refers to --help, as follows:

 For convenience, here are two example command line statements:

./mongofiles --uri='mongodb://mongouser:qidian%[email protected]:27017' --db db_shuozhuo put service.yaml

./mongofiles --host 11.186.6.xxx:27017 -u mongouser -p wexin@mongo -d db_shuozhuo put build_vs_env.sh

There is also a simple way to verify that the upload was successful. One is to look at the output after execution, and the other is to see if there is a corresponding entry in the target db. As shown in the figure below, you can see what is actually stored in the two collections.

some thinking.

        Although gridfs can store files, it means that the business does not need to introduce additional file storage (such as cos), and it also has the advantages of easy horizontal expansion. But I personally feel that gridfs is not very practical. The first is cost. For pictures and files, it is more professional to use special object storage (cos) directly, and the cost is still low, so it is not so natural to use a database. The other is performance. Accessing files from mongodb is slower than accessing files directly from the file system (middle layer overhead). In addition, if you want to modify the document on GridFs, you must first delete it and save it again.

After looking at other people on the Internet, they have similar opinions:   If you want to make a file system here , it is best to use a professional one. For example, TFS (Taobao File System, for a large number of random access to small pictures), FastDFS, mooseFS, etc.

        Of course, it should still be meaningful for small projects such as games. For example, only MongoDB can store messages, files, and act as redis. One database can meet all needs, simplify engineering code, and greatly reduce the same time. The cost of various storage transactions.

Guess you like

Origin blog.csdn.net/mijichui2153/article/details/118977823