Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store an image in MongoDB using Node.js/Express and Mongoose

Currently I handle image uploads using angular-file-upload and I simply save the image to the server's file system and reference it in the HTML. However, I want to try and store the image directly in the database within the Schema I defined for my blog posts.

var blogSchema = new Schema({     title: String,     author: String,     body: String,     likes: { type: Number, default: 0 },     comments: [{ type: Schema.Types.ObjectId, ref: 'Comment' }],     date: { type: Date, default: Date.now },     imageURL: String   // instead of this      image: // store it directly });  "imageURL: String" stores the path to the image. 

I want to make it so that I can just have a field that stores the image itself. I was thinking that I could perhaps just upload the image like I already do, but instead convert the image after it has been uploaded and store it in binary (or some other form) in Mongo. Is this possible?

Thanks!

like image 678
Bryan Cho Avatar asked Apr 21 '15 18:04

Bryan Cho


People also ask

Can Mongoose store images?

So for storing an image in MongoDB, we need to create a schema with mongoose. For that create the file `model. js` file and define the schema. The important point here is that our data type for the image is a Buffer which allows us to store our image as data in the form of arrays.

Can we store image in MongoDB?

The short answer is: Yes, you can store (small) images if you encode them correctly with base64 , see stackoverflow.com/questions/11442356 I also found menge.io/2015/03/24/storing-small-images-in-mongodb a very good starting point.


1 Answers

This example below shows how to upload an image to MongoDB using mongoose. Click this link for the original source

var express = require('express'); var fs = require('fs'); var mongoose = require('mongoose'); var Schema = mongoose.Schema;  var imgPath = '/path/yourimage.png';  mongoose.connect('localhost', 'testing_storeImg');  var schema = new Schema({     img: { data: Buffer, contentType: String } });  var A = mongoose.model('A', schema);  mongoose.connection.on('open', function () {   console.error('mongo is open');    A.remove(function (err) {     if (err) throw err;      console.error('removed old docs');      // store an img in binary in mongo     var a = new A;     a.img.data = fs.readFileSync(imgPath);     a.img.contentType = 'image/png';     a.save(function (err, a) {       if (err) throw err;        console.error('saved img to mongo');        // start a demo server       var server = express.createServer();       server.get('/', function (req, res, next) {         A.findById(a, function (err, doc) {           if (err) return next(err);           res.contentType(doc.img.contentType);           res.send(doc.img.data);         });       });        server.on('close', function () {         console.error('dropping db');         mongoose.connection.db.dropDatabase(function () {           console.error('closing db connection');           mongoose.connection.close();         });       });        server.listen(3333, function (err) {         var address = server.address();         console.error('server listening on http://%s:%d', address.address, address.port);         console.error('press CTRL+C to exit');       });        process.on('SIGINT', function () {         server.close();       });     });   });  }); 
like image 175
Alex Avatar answered Sep 20 '22 06:09

Alex