Storing Pictures/Icons in MongoDB


#1

Hello, everyone.

The code below I have developed is a simple controller module that suppose to push the deleted pictures into the MongoDB database collection. Obviously, it’s not working because the property item in the shema object is set to String. In the database, the picture is saved in JSON format with the following junk like that…

{
“_id”: {
“$oid”: “5b3d60273662261d0c0af9b6”
},
“item”: “/assets/Photo1.JPG”,
“__v”: 0
}

It works with the text-styled objects, but not with images. Is anyone can help me on this? Is it possible to store images in the MongoDB database? If yes, how it can be implemented by the code? Looks like the pictures cab be converted to a binary format called BSON, but how this can be done in the code?

var mongoose = require("mongoose");

mongoose.connect("mongodb://xxxxxx:xxxxxxx@ds125381.mlab.com:25381/images");

 //create an instance of the mongoose.Schema module...

var gallerySchema = new mongoose.Schema({
    item: String
});

//Mongoose Model for creating a Document in MongoDB with the properties defined by the schema above...

var Image = mongoose.model("Image", gallerySchema);
var pictureOne = Image({item: "/assets/Photo1.JPG"}).save(function(err){
    if (err) throw err;
    console.log("picture saved...");
});

//get handler to render the app output with the photogallery.ejs view template...

module.exports = function(app){
    app.get("/photogallery", function(req, res){
        res.render("photogallery");
    });
};