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 URL of the uploaded image in the MongoDB.
GridFS is the MongoDB specification for storing and retrieving large files such as images, audio files, video files, etc. It is kind of a file system to store files but its data is stored within MongoDB collections. GridFS has the capability to store files even greater than its document size limit of 16MB.
There are three ways to store the images in MongoDB: GridFS, Within Document, and Referencing an external URL. If an image or any binary file which is of size less than 16MB can be stored in MongoDB using Binarydata ( bindata ) type.
A database gives you the opportunity to store photos and other small images in a database table. You can create such a database table for example when you want to create an online photo album with descriptions of your photos. Storing images in a database table is not recommended.
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.
"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.
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();
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
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();
});
You can use the bin data type if you are using any scripting language to store files/images in MongoDB. Bin data is developed to store small size of files.
Refer to your scripting language driver. For PHP, click here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With