Store images in mongoDB

Hey guys, how can I store images in mongoDB? I read about GridFS and the documentation says:

In MongoDB, use GridFS for storing files larger than 16 MB.

But my images do not exceed 5mb each, so I’m not sure if using GridFS would be the best approach for this scenario.

Just save it as a either a binary Blob, or a base64-encoded string (note latter is about ~37% larger as far as i remember). There shouldn’t be any reason why you can’t just save them, you shouldn’t need a specialised solution for this.

edit, ah looks like blobs are a little bit of a pita, recommendation I’ve now seen a few times after reading up is that it should be sent to your server first, then the server deals with uploading the blob. However that might not be ideal, depends on your situation.

There seems to be conflicting info on the Mongo site re GridFS, some of the docs say anything over 4Mb, then GridFS says it’s for stuff over 16Mb. ech. Aaaanyway:

Furthermore, if your files are all smaller than the 16 MB BSON Document Size limit, consider storing each file in a single document instead of using GridFS. You may use the BinData data type to store the binary data. See your drivers documentation for details on using BinData.

I’m completely new to backend stuff and this simple task seems very opinionated. There’s GridFS, binary, etc, then you see people saying storing images in the database is a bad a idea and a best approach would be to store them on a cloud.

I just want the object in my schema to have an image property so I can have access to it on the client.

This is my schema:

const productSchema = new mongoose.Schema({
  productName: String,
  path: String,
  price: Number,
  category: String,
  description: String,
  onSale: Boolean,
  sale: Number

const Product = mongoose.model("Product", productSchema);

async function createProduct() {
  const product = new Product({
    productName: "Haori Jacket",
    path: "haori-jacket",
    price: 210,
    category: "Outwear",
      "Sed ornare tellus non lectus blandit faucibus. Curabitur convallis nibh ut libero lobortis ullamcorper. Aliquam ornare risus in orci iaculis egestas. Vivamus varius ipsum eu leo euismod, id fringilla lectus laoreet.",
    onSale: false


Using base64-encoded string takes around 30% more space, I am going to have like 40 of these items, would it be terrible to use base64-encoded string for this scenario?

No, there isn’t really a problem storing images in a database: the issue with images and databases is that images take a lot of space — the common issue is if users can upload images, it’s really easy to blow through the storage really quickly, but you aren’t going to have that issue.

Storing images on a filesystem (like Amazon S3) is more common, but it introduces a lot of complexity, the advantage to them being in the DB is that your life will be hella easier. I’ve always reflexively avoided storing images in a DB, but I think that’s cargo culting the more I read up on this (your question has been super helpful to me btw!).

One thing that seems to be suggested in the docs re mongo is to save images as their own document. I don’t use Mongo, but I assume the theory is the same as with a relational DB — you’d have images saved separately to the rest of the stuff, then in your Product document you have there, you have a field that would reference the ID of the I document. Not sure how well that works — someone with a bit more experience of Mongo might be able to confirm/deny that.