Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

store files in mongodb using mongoose

I am new with node and mongoDB. I am developing an app with nodeJS, express and mongoDB. I want read a csv/xlsx file from a file input field and store it in mongoDB using mongoose. I am having difficulties. I am using angularjs in my front end. Can anyone give me suggestions about what procedure I should go through? Specific Code will be great help.

I used busboy module to store files in a specific folder. Here is my code

In routes:

router.post('/fileupload', function (req, res) {
    var fstream;
    req.pipe(req.busboy);
    console.log(req.pipe);
    console.log(req.busboy);
    req.busboy.on('file', function (fieldname, file, filename) {
        console.log("Uploading: " + filename);
        fstream = fs.createWriteStream('./files/' + filename);
        file.pipe(fstream);
        fstream.on('close', function () {
            res.redirect('back');
        });
    });
});

and my frontend:

<form method="post" action="/fileupload" enctype="multipart/form-data">
    <input type="file" id="file" name="file">
    <button type="submit">Submit</button>
</form>

Up to now there is no error. Now i just want to store these files in database. What should I do next?

like image 418
ishakya Avatar asked Nov 27 '14 09:11

ishakya


People also ask

How do I store files in MongoDB?

In MongoDB, use GridFS for storing files larger than 16 MB. In some situations, storing large files may be more efficient in a MongoDB database than on a system-level filesystem. If your filesystem limits the number of files in a directory, you can use GridFS to store as many files as needed.

How does a Mongoose store data?

Mongoose | save() Function The save() function is used to save the document to the database. Using this function, new documents can be added to the database.

Can I use Mongoose with MongoDB?

Mongoose is an ODM (Object Data Modeling) library for MongoDB. While you don't need to use an Object Data Modeling (ODM) or Object Relational Mapping (ORM) tool to have a great experience with MongoDB, some developers prefer them.


1 Answers

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

like image 50
SOJ Avatar answered Oct 14 '22 05:10

SOJ