Java: Getting started to mastering-Database: Mongo DB GridFS file system, mongogridfs
I. Overview
GridFS is a storage mechanism of MongoDB used to store large binary files.
Advantages:
1. Using GridFS can simplify your stack. If you are using MongoDB, you can use GridFS instead of an independent file storage tool.
2. GridFS automatically balances existing copies or sets automatic shards for MongoDB. Therefore, it is easier to perform failover or horizontal scaling for file storage.
3. When used to store files uploaded by users, GridFS can easily solve problems that may occur in other file systems.
4. In GridFS, the concentration of file storage is very high, because MongoDB allocates data files in 2 GB.
Disadvantages:
1. GridFS performance is relatively low: Accessing files from MongoDB is not as fast as accessing files directly from the file system
2. If you want to modify a document on GridFS, you can only Delete the existing file and save the entire document again.
The simplest way to use GridFS is to use the program files tool. All Mongo DB distributions contain program files, you can use it to upload, download, view, search, and delete objects in GridFS. The following is an example.
Ii. Code demonstration.
2.1 upload a file
Program Files-d foobar-l "E: \ a.txt" put "a.txt"
2.2 view the file list
Db. fs. chunks. find () and db. fs. files. find () store all file information of the file system.
2.3 view all files
Program Files-d foobar list
2.4 delete an existing file
Program Files-d foobar delete 'a.txt'
Iii. Summary.
The GridFS system provides an efficient system for us to store large data files with MongoDB.