5 / 5
Sep 2024

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.

Do I need the grid system for small files. Under 2 MB?

Also a code example of the post. Python preferred.

You might as well use the grid file system if you’re storing opaque objects in MongoDB.

image

MongoDB’s AI expert will write you a very nice example in Python!

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()