title: Inserting an Image to MongoDB through Node.js
body: <p>I am having trouble inserting an image to MongoDB through node.js. My code is as follows:</p>
  
  <pre><code>var express  = require('express'),
  mongoose = require('mongoose'),
  fs = require('fs'),
  Schema = mongoose.Schema;
  app  = express(),
  port = process.env.PORT || 3000;
  
  var imagePath = '~\images\image1.png';
  
  mongoose.connect('mongodb://localhost/test');
  var db = mongoose.connection;
  
  var contentSchema = new Schema({
      _id: Schema.ObjectId,
      date: {type: Date, default: Date.now},
      user_ip: Number,
      likes: Number,
      reports: Number,
      media: {data: Buffer, contentType: String}
  })
  
  var Content = mongoose.model('content', contentSchema);
  
  var media = new Content({
      user_ip:12345,
      likes: 1400,
      reports: 0,
      media: fs.readFileSync(imagePath)
  });
  
  media.save(function(err,media){
      if (err) return console.error(err);
      console.dir(media);
  });
  </code></pre>
  
  <p>I believe what I am missing is how I am trying to upload the media, i.e. fs.readFileSync(imgpath). Is this the common approach, or is there a better one that you recommend?</p>
  
  <p>Alternatively, I am open to storing the images in a folder on my server and simply adding the URL linked to the images to the JSON object in my database. This way, I do not have to worry about actually storing any media into Mongo. Does one way provide better performance than the other?</p>