store files in mongodb using mongoose store files in mongodb using mongoose express express

store files in mongodb using mongoose


You probably will need to understand the basis for storing files in mongo. Why are files stored in mongo and how they are stored?

From what you have done, you now need a mongoose plugin that will store your uploaded file into mongo GridFS. The GridFS is here:- http://docs.mongodb.org/manual/core/gridfs/. You can use any compatible driver to access the grid - mongoose and mongoose plugin gridfs-stream are an example - see here: https://www.npmjs.org/package/gridfs-stream

I have used below to save a file into the gridfs.

var express = require('express');var formidable = require('formidable');var mongoose = require('mongoose');var grid = require('gridfs-stream');var fs = require('fs');var util = require('util');var app = express();app.post('/fileupload', function (req, res) {    var form = new formidable.IncomingForm();    form.uploadDir = __dirname + "/data";    form.keepExtensions = true;    form.parse(req, function(err, fields, files) {        if (!err) {          console.log('File uploaded : ' + files.file.path);          grid.mongo = mongoose.mongo;          var conn = mongoose.createConnection('..mongo connection string..');          conn.once('open', function () {          var gfs = grid(conn.db);          var writestream = gfs.createWriteStream({              filename: files.file.name          });          fs.createReadStream(files.file.path).pipe(writestream);       });     }           });   form.on('end', function() {               res.send('Completed ..... go and check fs.files & fs.chunks in  mongodb');   });});app.get('/', function(request, response){    response.send(        '<form method="post" action="/fileupload" enctype="multipart/form-data">'        + '<input type="file" id="file" name="file">'        + '<input type="submit" value="submit">'        + '</form>'        );    });app.listen(40000, function() {    console.log('Express is listening on port 40000');});

The above is a guide on how you should proceed after uploading your file and it is not a production ready proof of concept. Note that I replaced busboy with formidable as a personal preference.

Does it help you to move on?

SO1