ChatGPT解决这个技术问题 Extra ChatGPT

Store images in a MongoDB database

How can I store images in a MongoDB database rather than just text? Can I create an array of images in a MongoDB database? Will it be possible to do the same for videos?

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.

G
Gates VP

Please see the GridFS docs for details on storing such binary data.

Support for your specific language should be linked to at the bottom of the screen.


GridFS is for documents > 16MB. Not all binary data is this large. Other than being able to surpass that limit, is there any other benefits to using GridFS instead of just the BinData type?
Videos will tend to be > 16MB so this answer is probably reasonable. Should perhaps mention this though.
any other solution, rather then using GridFS?
@AbhishekMani the other solution is not to store them in MongoDB at all. Now, in 2018, it's infinitely easier to write this data to something like Amazon's S3 or Azure BLOB storage.
@GatesVP can you reply to Brandon question "GridFS is for documents > 16MB. Not all binary data is this large. Other than being able to surpass that limit, is there any other benefits to using GridFS instead of just the BinData type?" Thanks in advance.
P
Peter Mortensen

"You should always use GridFS for storing files larger than 16MB" - When should I use GridFS?

MongoDB BSON documents are capped at 16 MB. So if the total size of your array of files is less than that, you may store them directly in your document using the BinData data type.

Videos, images, PDFs, spreadsheets, etc. - it doesn't matter, they are all treated the same. It's up to your application to return an appropriate content type header to display them.

Check out the GridFS documentation for more details.


P
Peter Mortensen

You can try this one:

String newFileName = "my-image";
File imageFile = new File("/users/victor/images/image.png");
GridFS gfsPhoto = new GridFS(db, "photo");
GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile);
gfsFile.setFilename(newFileName);
gfsFile.save();

C
Community

http://blog.mongodb.org/post/183689081/storing-large-objects-and-files-in-mongodb

There is a Mongoose plugin available on NPM called mongoose-file. It lets you add a file field to a Mongoose Schema for file upload. I have never used it but it might prove useful. If the images are very small you could Base64 encode them and save the string to the database.

Storing some small (under 1MB) files with MongoDB in NodeJS WITHOUT GridFS


f
flvps

install below libraries

var express = require(‘express’);
var fs = require(‘fs’);
var mongoose = require(‘mongoose’);
var Schema = mongoose.Schema;
var multer = require('multer');

connect ur mongo db :

mongoose.connect(‘url_here’);

Define database Schema

var Item = new ItemSchema({ 
    img: { 
       data: Buffer, 
       contentType: String 
    }
 }
);
var Item = mongoose.model('Clothes',ItemSchema);

using the middleware Multer to upload the photo on the server side.

app.use(multer({ dest: ‘./uploads/’,
  rename: function (fieldname, filename) {
    return filename;
  },
}));

post req to our db

app.post(‘/api/photo’,function(req,res){
  var newItem = new Item();
  newItem.img.data = fs.readFileSync(req.files.userPhoto.path)
  newItem.img.contentType = ‘image/png’;
  newItem.save();
});

Sir what if I want to add array of Images
@NikhilPatil, you can use something like this: var upload = multer({ dest: 'uploads/' }); app.post('/api/photos/', upload.array('photos', 12), function(req, res) {...}) see the doc on Multer
How can I display image from mongo
P
Peter Mortensen

You can use the bin data type if you are using any scripting language to store files/images in MongoDB. Bin data is developed to store small size of files.

Refer to your scripting language driver. For PHP, click here.


P
Peter Mortensen
var upload = multer({dest: "./uploads"});
var mongo = require('mongodb');
var Grid = require("gridfs-stream");
Grid.mongo = mongo;

router.post('/:id', upload.array('photos', 200), function(req, res, next){
gfs = Grid(db);
var ss = req.files;
   for(var j=0; j<ss.length; j++){
     var originalName = ss[j].originalname;
     var filename = ss[j].filename;
     var writestream = gfs.createWriteStream({
         filename: originalName
     });
    fs.createReadStream("./uploads/" + filename).pipe(writestream);
   }
});

In your view:

<form action="/" method="post" enctype="multipart/form-data">
<input type="file" name="photos">

With this code you can add single as well as multiple images in MongoDB.


N
NeNaD

Can you store files in MongoDB?

Yes.

Should you store files 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.