Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

store images in mongodb using mongoose?? how to?

anybody has a quick example to insert an image in mongodb using mongoose and nodejs express

i read some examples but i cant understand how to??

i want to upload an image in a form

app.post('/videos/new', function(req, res) {
  req.form.complete(function(err, fields, files) {
    console.log('here i go');
    if(err) {
      next(err);
    } else {
      ins = fs.createReadStream(files.file.path);
      console.log('insssssssssssss'+ins);
      ous = fs.createWriteStream(__dirname + '/static/uploads/videos/' + files.file.filename);
      util.pump(ins, ous, function(err) {
        if(err) {
          next(err);
        } else { RegProvider.save({
           file: req.param(files.file.filename),
                   filename: req.param('filename')
                 }, function(error, docs) {
              res.redirect('/videos');
         });
        }
      });
      //console.log('\nUploaded %s to %s', files.file.filename, files.file.path);
      //res.send('Uploaded ' + files.file.filename + ' to ' + files.file.path);
    }
  });
});

like this example but i dont understand how it works

like image 569
andrescabana86 Avatar asked Aug 20 '12 23:08

andrescabana86


People also ask

Can I store images in MongoDB?

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.

How use MongoDB With Mongoose?

You can connect to MongoDB with the mongoose.connect() method. mongoose.connect('mongodb://localhost:27017/myapp'); This is the minimum needed to connect the myapp database running locally on the default port (27017). If connecting fails on your machine, try using 127.0.0.1 instead of localhost .


1 Answers

util.pump is deprecated.

You might try out https://github.com/aheckmann/gridform or https://github.com/aheckmann/connect-multipart-gridform and see if they meet your needs.

like image 83
aaronheckmann Avatar answered Sep 30 '22 01:09

aaronheckmann