Uploading Files to MongoDB With GridFS (Node.js App)
Why Stack Overflow recommends it
Stack Overflow users link it as the walkthrough they followed when building GridFS file uploads in Node, and also when asking why parts of it break on newer library versions.
Recommended by 3 people across 2 different months, 2020 to 2023.
What it is
A 55-minute tutorial from Traversy Media that builds a Node.js and Express app for uploading files of any type to MongoDB using GridFS. It uses Multer, the GridFS storage engine, gridfs-stream and the EJS template engine, with the code available in a GitHub repo.
What people said
Recommends Sheece Gardazi · 2020-06 · Stack Overflow · on “how to replace gridStore to gridFSBucket?”
I followed this tutorial [this video] to create this recipe. Tutorial is awesome it explains all the steps quite nicely. Complete code example can be found here [LINK] . Html form example: <form action=" [LINK] " method="post" enctype="multipart/form-data"> <input type="file" name='image' /> <button type="submit" >Submit</button> </form> Controller recipe to upload image to mongodb: const path = require('path'); const crypto = require('crypto'); const mongoose = require('mongoose'); const multer = require('multer'); const GridFsStorage = require('multer-gridfs-storage'); const Grid = require('gridfs-stream'); const mongodbURL="mongodb+srv://<user>:<pass>@cluster.mongodb.net/<databaseName>" const
Recommends turivishal · 2020-06 · Stack Overflow · on “How to upload image to user profile using angular express multer mongoose”
I am not attaching any code example here because its not cleared which method you prefer to develop this feature, There two methods if you want to store in mongoDB database, GridFS mongoDB reference docs, [LINK] recommending a demo, uploading-files-to-mongodb-with-gridfs-and-multer-using-nodejs [LINK] [this video] BSON mongoDb reference docs, [LINK] recommending a demo, how-to-store-images-to-mongodb-with-node-js [LINK] [LINK] (you can find both type of image upload examples and demo of node.js in below link) General reference: [LINK] Difference: GridFS. GridFS is a specification for storing and retrieving files that exceed the BSON-document size limit of 16 MB. ... Instead of storing a file in a
Recommends nolram · 2023-07 · Stack Overflow · on “How should I retrieve files from GridFs? gfs.files.find() does not work and loading forever”
I also having this issue. I followed the same youtube tutorial [this video] and got stuck for days. Then I tried this code below and then it worked for me. IDK what happened there. Hope this will work for you. app.get("/files", async (req, res) => { await gfs.files.find().toArray((err, files) => { if (err) return res.status(400).json({ err }); console.log({files}); }); });
Cites CodingPep · 2023-06 · Stack Overflow · on “How should I retrieve files from GridFs? gfs.files.find() does not work and loading forever”
I am trying to display all the files, so I want to retrieve all the files first as arrays, so that I can mapping on frontend part. I tried to solve this issue by [this video] . Seems like there has been a lot of update on GridFs since when the video was made. I tried all the solutions on the reply of the video above, and solutions on stackoverflow too. None of them worked unfortunately. So this is the part showing what modules I use, and how I connect to MongoDB, how to initilize GFS, and so on. const path = require('path'); const express = require('express'); const app = express(); const multer = require('multer'); const Grid = require('gridfs-stream'); const {GridFsStorage} = require('multer-gr