GridFS with MongoDB
The MBS Xojo MongoDB Plugin got GridFS functionality in the last release. It allows you to store files in a MongoDB database. Internally the files are stored in two tables, one with the file list and a second one with file chunks. The MongoDB client library provides a nice interface for this. We wrap that in MongoGridFSMBS and MongoGridFSFileMBS classes.
Read file
Let's read a file. We build the MongoURIMBS object for whatever server we need, in our example a local one. We connect to the server using the MongoClientMBS class. Then we initialise the MongoGridFSMBS object using the name of the database "test". We store files in this database and use the prefix "fs" for the table. You can store files in multiple databases and store different file sets within the same database with different prefixes.
The FindOneByFileName() function finds a file by the name (or path). You get the matching MongoGridFSFileMBS object and then you can inspect properties or use the read methods to read the content. Here is a little sample code:
You may have the Client and GridFS objects in properties of your window or app. This way you keep it open and avoid reconnecting each time.
List files
We can list all files with running a Find command passing search criteria as as JSON object. In our example we pass {} to get all files:
Optionally you can include options there, for example use a regular expression to find all files starting with the word test:
{ "filename": { "$regex": "^test" } }
Or find files with more than 10000 bytes:
{ "length": { "$gt": 10000 } }
Write file
To write a file, you have two ways. We can create a stream for an existing file on disk and use that stream to create a new file using CreateFileFromStream() function. The library then streams data in chunks to the server.
Alternatively you can create a MongoGridFSFileMBS object and use the Write methods there to add data. For example if you generate a JPEG image from a Picture with Picture.ToData() function and pass that in memory to the Write() method.
The MongoDB GridFS functions allow you to store files in the database and access them from various clients. Great to store documents or images within the database and exceed the 16 MB limit for binary fields. If you e.g. include PDF documents with records, you could store them with the record and report an error to the user if the size is too big. Or you store all of the files in GridFS and reference them from the records.
Let us know if you have questions.
