Complete developer guide to storing, querying, and managing files in MongoDB GridFS
Introduction
GridFS is MongoDB’s mechanism for storing large files by splitting them into chunks and storing them across two collections. The MBS FileMaker Plugin exposes a complete API for working with GridFS directly from FileMaker scripts.
This allows developers to treat MongoDB as a scalable file storage backend for images, documents, and binary data.
1. Opening GridFS
MBS( "MongoDB.GridFS.Open"; MongoDBRef; db; prefix )
This initializes a GridFS bucket inside a MongoDB database. The prefix defines the internal collections used for file and chunk storage. You must open GridFS before performing any file operations.
# FileMaker Script Example
# start a new session
Set Variable [ $Mongo ; Value: MBS( "MongoDB.New" ) ]
# where is the server?
Set Variable [ $r ; Value: MBS( "MongoDB.SetURI"; $Mongo; "mongodb://localhost/" ) ]
# connect
Set Variable [ $r ; Value: MBS( "MongoDB.Connect"; $Mongo) ]
# open GridFS
Set Variable [ $r ;
Value: MBS( "MongoDB.GridFS.Open"; $Mongo; "files" )
]
If [ MBS("IsError") ]
Show Custom Dialog [ "Error opening GridFS" ; $r ]
Exit Script
End If
2. Writing Files
MBS( "MongoDB.GridFS.Write"; MongoDBRef; Container ; Options )
Stores a FileMaker container as a GridFS file. This is ideal for images, PDFs, and any binary data. You can optionally define filename, content type, and metadata for later filtering.
# FileMaker Script Example
Set Variable [ $file ; Value: Table::Image ]
Set Variable [ $r ;
Value: MBS( "MongoDB.GridFS.Write";
$Mongo;
$file;
"{\"filename\":\"photo.jpg\",\"contentType\":\"image/jpeg\"}"
)
]
If [ MBS("IsError") ]
Show Custom Dialog [ "Upload failed" ; $r ]
End If
3. Reading Files
MBS( "MongoDB.GridFS.Read"; MongoDBRef; ID )
Reads a file from GridFS using its unique ID and returns it as a FileMaker container value. This is the primary way to retrieve stored binary data.
# FileMaker Script Example
Set Variable [ $fileID ; Value: "6a1836306f6e4b6f9e03f46d" ]
Set Field [ Table::Image ;
MBS( "MongoDB.GridFS.Read"; $Mongo; $fileID )
]
4. Finding Files
Find One by Filename
MBS( "MongoDB.GridFS.FindOneByFileName"; MongoDBRef; Filename )
Finds the first file matching a given filename. Useful for simple lookup scenarios where filenames are unique.
Find One (Flexible Query)
MBS( "MongoDB.GridFS.FindOne"; MongoDBRef; filter ; opts )
Allows advanced filtering using JSON queries such as file size, metadata, or custom fields. This is the most flexible search method in GridFS.
# FileMaker Script Example
Set Variable [ $r ;
Value: MBS( "MongoDB.GridFS.FindOne";
$Mongo;
"{\"filename\":\"photo.jpg\"}";
"{}"
)
]
Find Many Files
MBS( "MongoDB.GridFS.Find"; MongoDBRef; filter )
Returns all files matching a filter as a JSON array. This is useful for file browsers or admin tools.
# FileMaker Script Example
Set Variable [ $list ;
Value: MBS( "MongoDB.GridFS.Find"; $Mongo; "{}" )
]
Show Custom Dialog [ "Files" ; $list ]
5. Removing Files
Remove by ID
MBS( "MongoDB.GridFS.Remove"; MongoDBRef; ID )
Remove by Filename
MBS( "MongoDB.GridFS.RemoveByFileName"; MongoDBRef; Filename )
These functions delete both the file metadata and its chunks. Removing by filename will affect all matching files.
# FileMaker Script Example
Set Variable [ $r ;
Value: MBS( "MongoDB.GridFS.Remove"; $Mongo; $fileID )
]
6. Inspecting GridFS Internals
Returns the metadata collection for all stored files. This allows you to query GridFS using standard MongoDB collection functions.
Returns the internal chunk storage collection. This is useful for debugging or advanced storage inspection.
# FileMaker Script Example
Set Variable [ $r ; Value: MBS("MongoDB.GridFS.GetFiles"; $Mongo) ]
Set Variable [ $files ; Value: MBS("MongoDB.Find"; $Mongo; "{}") ]
These functions are primarily for advanced use cases. Most applications only interact with the higher-level GridFS API.
7. Dropping GridFS
MBS( "MongoDB.GridFS.Drop"; MongoDBRef )
Drops the entire GridFS bucket, including all files and chunks. This is a destructive operation used for resets or test cleanup.
# FileMaker Script Example
Set Variable [ $r ;
Value: MBS( "MongoDB.GridFS.Drop"; $Mongo )
]
8. Closing GridFS
MBS( "MongoDB.GridFS.Close"; MongoDBRef )
Closes the GridFS context. In most cases this is not required because MongoDB.Release automatically cleans up resources.
# FileMaker Script Example
Set Variable [ $r ;
Value: MBS( "MongoDB.GridFS.Close"; $Mongo )
]
Conclusion
The MBS FileMaker Plugin provides a complete and production-ready interface to MongoDB GridFS. It enables FileMaker solutions to handle large files efficiently without relying on external file systems. And you benefit from automatically synchronization between various MongoDB servers.
With full support for querying, metadata filtering, and chunk-based storage, GridFS becomes a powerful backend for document management, media libraries, and archival systems.