KamranV21
KamranV21

Reputation: 83

Slow image loading from MongoDB

I'm creating a small MERN stack app (using Atlas database) where users can upload multiple images and then view them in some sort of gallery.

Currently, I upload images using GridFS.

The problem is that when I try to get for instance a single image from the database it takes too much time until I get a response: about a second for a small 200x200 picture when usually it takes 50 ms or even less.

Here is my route for getting an image:

router.get('/photo/single/:filename', (req, res) => {
  const bucket = new mongoose.mongo.GridFSBucket(mongoose.connection.db, {
    chunkSizeBytes: 1024,
    bucketName: 'uploads'
  })

  bucket
    .openDownloadStreamByName(req.params.filename).pipe(res)
})

Not much code here as you can see.

So why does openDownloadStreamByName work so slow in my case? Is it just because I'm using a free M0 cluster or I'm just missing something in my route? Or maybe storing images in MongoDB is not a good idea by itself (but I don't see another solution since in my case images can weight more that 16MB)?

Upvotes: 0

Views: 1304

Answers (2)

KamranV21
KamranV21

Reputation: 83

I gave up on the idea of storing files in MongoDB. Instead, I've created a bucket on AWS S3 and it works fine.

Upvotes: 1

Techgeek
Techgeek

Reputation: 260

Hi please store images and other binary data in MongoDB Atlas using GridFS.

Upvotes: 0

Related Questions