How can I store images in a MongoDB database rather than just text? Can I create an array of images in a MongoDB database? Will it be possible to do the same for videos?
-
3The short answer is: Yes, you can store (small) images if you encode them correctly with `base64`, see https://stackoverflow.com/questions/11442356/ I also found http://menge.io/2015/03/24/storing-small-images-in-mongodb/ a very good starting point. – B--rian Jan 29 '19 at 10:01
8 Answers
Please see the GridFS docs for details on storing such binary data.
Support for your specific language should be linked to at the bottom of the screen.

- 44,957
- 11
- 105
- 108
-
55GridFS is for documents > 16MB. Not all binary data is this large. Other than being able to surpass that limit, is there any other benefits to using GridFS instead of just the BinData type? – just a slime Jan 29 '15 at 16:32
-
6Videos will tend to be > 16MB so this answer is probably reasonable. Should perhaps mention this though. – Chanoch Apr 26 '17 at 13:11
-
2
-
3@AbhishekMani the other solution is not to store them in MongoDB at all. Now, in 2018, it's infinitely easier to write this data to something like Amazon's S3 or Azure BLOB storage. – Gates VP Nov 01 '18 at 16:40
-
-
1@GatesVP can you reply to Brandon question "GridFS is for documents > 16MB. Not all binary data is this large. Other than being able to surpass that limit, is there any other benefits to using GridFS instead of just the BinData type?" Thanks in advance. – Nux May 18 '19 at 09:24
-
2Hey @Nux, this question is from 2011, that's over 8 years ago. In 2019, the answer to Brandon's question is irrelevant. Don't use MongoDB for storing binary data like this, we have _way_ better solutions for storing (_and streaming_) data efficiently. – Gates VP May 19 '19 at 22:45
-
@GatesVP, I use Azure Blob Storage + Azure CDN to store and distribute images on my website https://www.indievisible.net – programad Oct 30 '19 at 16:39
-
@BrandonFitzpatrick very old comment I know, but the docs say that GridFS is _suggested_ for files over 16MB. There's no minimum file size requirement. – Madbreaks May 26 '20 at 15:42
"You should always use GridFS for storing files larger than 16MB" - When should I use GridFS?
MongoDB BSON documents are capped at 16 MB. So if the total size of your array of files is less than that, you may store them directly in your document using the BinData data type.
Videos, images, PDFs, spreadsheets, etc. - it doesn't matter, they are all treated the same. It's up to your application to return an appropriate content type header to display them.
Check out the GridFS documentation for more details.

- 30,738
- 21
- 105
- 131

- 8,196
- 2
- 43
- 63
You can try this one:
String newFileName = "my-image";
File imageFile = new File("/users/victor/images/image.png");
GridFS gfsPhoto = new GridFS(db, "photo");
GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile);
gfsFile.setFilename(newFileName);
gfsFile.save();

- 30,738
- 21
- 105
- 131

- 426
- 4
- 8
http://blog.mongodb.org/post/183689081/storing-large-objects-and-files-in-mongodb
There is a Mongoose plugin available on NPM called mongoose-file. It lets you add a file field to a Mongoose Schema for file upload. I have never used it but it might prove useful. If the images are very small you could Base64 encode them and save the string to the database.
Storing some small (under 1MB) files with MongoDB in NodeJS WITHOUT GridFS

- 1
- 1

- 9,214
- 3
- 21
- 17
install below libraries
var express = require(‘express’);
var fs = require(‘fs’);
var mongoose = require(‘mongoose’);
var Schema = mongoose.Schema;
var multer = require('multer');
connect ur mongo db :
mongoose.connect(‘url_here’);
Define database Schema
var Item = new ItemSchema({
img: {
data: Buffer,
contentType: String
}
}
);
var Item = mongoose.model('Clothes',ItemSchema);
using the middleware Multer to upload the photo on the server side.
app.use(multer({ dest: ‘./uploads/’,
rename: function (fieldname, filename) {
return filename;
},
}));
post req to our db
app.post(‘/api/photo’,function(req,res){
var newItem = new Item();
newItem.img.data = fs.readFileSync(req.files.userPhoto.path)
newItem.img.contentType = ‘image/png’;
newItem.save();
});

- 179
- 1
- 9

- 268
- 2
- 7
-
-
@NikhilPatil, you can use something like this: `var upload = multer({ dest: 'uploads/' }); app.post('/api/photos/', upload.array('photos', 12), function(req, res) {...})` [see the doc on Multer](https://github.com/expressjs/multer#usage) – Karatheodory Sep 15 '20 at 12:48
-
Can you store files in MongoDB?
Yes.
Should you store files in MongoDB?
No.
MongoDB is NOT a good place for storing files. If you want to store files, you should use storages like Amazon S3 or Google Could Storage.
The good practice is to store the files in a storage and then to just save the URI of the uploaded image in the MongoDB.

- 179,855
- 19
- 132
- 245

- 18,172
- 8
- 47
- 89
-
1While I agree, Cloud Storage isn't always an option. Thus why you can use [GridFS](https://www.mongodb.com/docs/manual/core/gridfs/) – OneCricketeer Jul 28 '22 at 15:27
var upload = multer({dest: "./uploads"});
var mongo = require('mongodb');
var Grid = require("gridfs-stream");
Grid.mongo = mongo;
router.post('/:id', upload.array('photos', 200), function(req, res, next){
gfs = Grid(db);
var ss = req.files;
for(var j=0; j<ss.length; j++){
var originalName = ss[j].originalname;
var filename = ss[j].filename;
var writestream = gfs.createWriteStream({
filename: originalName
});
fs.createReadStream("./uploads/" + filename).pipe(writestream);
}
});
In your view:
<form action="/" method="post" enctype="multipart/form-data">
<input type="file" name="photos">
With this code you can add single as well as multiple images in MongoDB.

- 30,738
- 21
- 105
- 131

- 56
- 4