I’ve googled and also read he documents here on Mongodb. Can someone show me an example of attaching a binary file in a Mongo record?
Been searching and trying for weeks.
Jack_Woehr
(Jack Woehr)
2
Hello @Harry_Melamed … You want the Grid File System feature of MongoDB.
1 Like
Do I need the grid system for small files. Under 2 MB?
Also a code example of the post. Python preferred.
Jack_Woehr
(Jack Woehr)
4
You might as well use the grid file system if you’re storing opaque objects in MongoDB.

MongoDB’s AI expert will write you a very nice example in Python!
Jack_Woehr
(Jack Woehr)
5
Anyway, here’s a simple example. Of course change the URI and filename to suit.
from pymongo import MongoClient
import gridfs
client = MongoClient("mongodb+srv://myid:mYpAsSw0rD@cluster0-foo.mongodb.net")
db = client.test
bucket = gridfs.GridFSBucket(db)
with open("myfile.txt", "rb") as f:
bucket.upload_from_stream("myfile", f)
client.close()
3 Likes
since these are small files, can the just be integrated as documents without the gridfs?
Well, you could. I tend to keep artifacts in the file system and just store a file system path in the document.