« MBS Xojo Developer Co… | Home | MBS Xojo Plugins, ver… »

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:

Sub Read() Var uri As New MongoURIMBS("mongodb://localhost/") Var Client as new MongoClientMBS(uri) Var GridFS As MongoGridFSMBS = Client.GridFS("test", "fs") // now find the file and write it Var file As MongoGridFSFileMBS = GridFS.FindOneByFileName( "test.txt") // read data Var data As String = file.ReadAll Var bytes As Integer = data.Bytes MessageBox "bytes read: "+bytes.ToString End Sub

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:

Sub ReadList() Var files() As MongoGridFSFileMBS = GridFS.Find2("{}") for each file as MongoGridFSFileMBS in files MessageBox file.FileName+" "+file.Length.ToString next End Sub

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.

Sub WriteWithStream() // test uploading a file Var options As New MongoGridFSFileOptionsMBS options.FileName = "test.pdf" options.ContentType = "application/pdf" Var stream As MongoStreamMBS = MongoStreamMBS.NewStreamFromPath("/Users/cs/Desktop/Installation.pdf") Var file As MongoGridFSFileMBS = GridFS.CreateFileFromStream(stream, options) file.FileName = "other.pdf" file.Save End Sub

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.

Sub WriteFile() // test uploading a file Var options As New MongoGridFSFileOptionsMBS options.FileName = "test.txt" options.ContentType = "text/plain" Var file As MongoGridFSFileMBS = GridFS.CreateFile(options) Var BytesWritten As Integer = file.Write("Hello World") file.Save End Sub

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.

The biggest plugin in space...
26 05 25 - 15:40