Working with GridFS
1. Understanding GridFS Architecture
| Collection | Purpose |
|---|---|
| fs.files | File metadata (one doc per file) |
| fs.chunks | Binary chunks (default 255KB each) |
| Index | fs.chunks: {files_id:1, n:1} unique |
| When to use | Files > 16MB OR need stream access to part of file |
2. Uploading Files
| API | Method |
|---|---|
| CLI | mongofiles put report.pdf |
| Driver (Node) | bucket.openUploadStream(filename, {metadata}) |
| Java | gridFSBucket.uploadFromStream(filename, in) |
const bucket = new GridFSBucket(db);
fs.createReadStream("./report.pdf").pipe(bucket.openUploadStream("report.pdf", { metadata: { owner: "alice" } }));
3. Downloading Files
| Method | Detail |
|---|---|
| openDownloadStream(id) | By _id |
| openDownloadStreamByName(name) | By filename (latest revision) |
| CLI | mongofiles get report.pdf |
4. Listing Files
| Method | Detail |
|---|---|
| bucket.find(filter) | Cursor over fs.files |
| CLI | mongofiles list |
5. Deleting Files
| Method | Effect |
|---|---|
| bucket.delete(id) | Removes file metadata + all chunks |
| CLI | mongofiles delete report.pdf |
6. Using GridFS with Language Drivers
| Driver | Class / Module |
|---|---|
| Node.js | GridFSBucket |
| Python (PyMongo) | gridfs.GridFSBucket |
| Java | com.mongodb.client.gridfs |
| Go | mongo.GridFSBucket |
| .NET | GridFSBucket<TFileId> |
7. Setting Chunk Size
| Option | Default |
|---|---|
| chunkSizeBytes | 261120 (255 KB) |
| Range | 1 byte → 16MB-1 |
| Trade-off | Larger = fewer chunks; smaller = better streaming |
8. Querying GridFS Files Collection
| Field | Description |
|---|---|
| _id | File id |
| length | Bytes |
| chunkSize | Per-chunk bytes |
| uploadDate | Date |
| filename | Name |
| metadata | User-defined |
9. Storing Metadata with Files
| Aspect | Detail |
|---|---|
| metadata field | Any subdocument |
| Indexable | Create indexes on metadata.* for search |
| Examples | owner, contentType, tags, checksum |
10. Handling Large File Storage
| Tip | Effect |
|---|---|
| Stream upload/download | Avoid loading entire file in memory |
| Parallel reads | Range download by chunk index |
| Alternative | S3/Blob storage + GridFS only for embedded use case |
11. Using GridFS for Binary Data
| Use case | Why GridFS |
|---|---|
| Files > 16MB | BSON doc limit |
| Partial reads | Chunk-level access |
| Replicated files | Same replication guarantees as data |
| Transactional with metadata | Files live in same DB |