40

I want to store image documents in MongoDB. I am using java.

Any links or suggestions would be appreciated.

roottraveller
  • 7,942
  • 7
  • 60
  • 65

3 Answers3

43

For storing binary data like images you can use GridFS or implement your own realization; Download the driver and look at src/test/com/mongodb/gridfs/GridFSTest.java ;)

Edit: you are lucky today! I made complete code for you;) Enjoy!

package mongodb.testing.java;
import com.mongodb.*;
import com.mongodb.gridfs.*;
import java.io.*;

public class Main {

    public static byte[] LoadImage(String filePath) throws Exception {
        File file = new File(filePath);
        int size = (int)file.length();
        byte[] buffer = new byte[size];
        FileInputStream in = new FileInputStream(file);
        in.read(buffer);
        in.close();
        return buffer;
    }

    public static void main(String[] args) throws Exception {
        //Load our image
        byte[] imageBytes = LoadImage("C:/Temp/bear.bmp");
        //Connect to database
        Mongo mongo = new Mongo( "127.0.0.1" );
        String dbName = "GridFSTestJava";
        DB db = mongo.getDB( dbName );
        //Create GridFS object
        GridFS fs = new GridFS( db );
        //Save image into database
        GridFSInputFile in = fs.createFile( imageBytes );
        in.save();

        //Find saved image
        GridFSDBFile out = fs.findOne( new BasicDBObject( "_id" , in.getId() ) );

        //Save loaded image from database into new image file
        FileOutputStream outputImage = new FileOutputStream("C:/Temp/bearCopy.bmp");
        out.writeTo( outputImage );
        outputImage.close();
    }
}
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Edward83
  • 6,664
  • 14
  • 74
  • 102
5

For small (<1mb) images I'd suggest storing them in a binary field (byte[] in java); if you have larger files GridFS will work better.

I store lots of thumbnails in my documents directly using a simple binary field.

I use Morphia (http://code.google.com/p/morphia) to store my POJOs.

Scott Hernandez
  • 7,452
  • 2
  • 34
  • 25
  • I am going to store lot of images in mongo. Both thumbnails and ordinary size. Is pojo essential for me??!!! –  Nov 23 '10 at 00:45
  • Can you tell me how do you represent images in mongo. like, { "x":1, "y":2 } how do you represent images???? –  Nov 23 '10 at 00:47
  • How you store your images? All in one document? or separate documents in one collections? or Say if i want to store multiple image in a document that looks like `{ "name" : "Tamil", "age" : 23, "Images"{"image1" : 123.jpg, "image2": abc.jpg}}` how shall i do it with java –  Nov 23 '10 at 01:11
2

Well, AFAIK, you cant store images in mongodb, you can store the links to images. And I am not high as I say this, if the images are small, like 100x100 px, U can try storing the image in binary, and reform and image on the fly. But, it will take some time to render the images, and hence I suggest you to save the link, and just fetch that image and populate it dynamically.

You can use GridFS to store larger binary objects.

Since you are new to mongoDB, take a look at:

Link 1

Link 2

Mongo+JAVA Tutorial

GridFS specs

You can also always ask on SO, I am also new to mongoDB, and wouldn't mind helping a newbie along :D

theTuxRacer
  • 13,709
  • 7
  • 42
  • 59
  • How do you link GridFS file with plain document in mongodb. Say, I am uploading an big image and i store it in GridFS. I save the other details of the file in a document in any collection. How can i retrieve the associated image, when i read the document from collection??? –  Nov 26 '10 at 08:37
  • 1
    i dont know if there is any API method to do it, you create a link between them, by creating a field in both collections, with the same values. So when you come across the value in the first collection, you jsut query the images collection with the value, and come across the corresponding image. – theTuxRacer Nov 26 '10 at 09:19