Discussion:
how to insert blob data into mongodb
Navneet Mathpal
2014-02-12 11:38:38 UTC
Permalink
Hi,

I am new in mongodb i have just started it.

I have just installed mongodb in my system,i want to insert a blob data in
mongodb how can i do it .

Thanks
Navneet Mathpal
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb

---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Eoin Brazil
2014-02-12 11:55:29 UTC
Permalink
Hi Navneet,

It depends on the size of your blob of data, you can store it in a document
using the binary data type (see this page for more info<http://docs.mongodb.org/manual/reference/bson-types/>)
or if the resulting document would be larger than 16MB you can use GridFS (see
this FAQ<http://docs.mongodb.org/manual/faq/developers/#faq-developers-when-to-use-gridfs>
).

Which language and driver are you using to add this data from your
application ? I can give a better answer regarding how you might do this
with this information.

Thanks!
Eoin
Post by Navneet Mathpal
Hi,
I am new in mongodb i have just started it.
I have just installed mongodb in my system,i want to insert a blob data in
mongodb how can i do it .
Thanks
Navneet Mathpal
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb

---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Navneet Mathpal
2014-02-12 12:36:55 UTC
Permalink
hi Eoin

I am using java and no idea about driver because i have just installed
mongodb, can u suggest..


Thanks
Navneet
Post by Eoin Brazil
Hi Navneet,
It depends on the size of your blob of data, you can store it in a
document using the binary data type (see this page for more info<http://docs.mongodb.org/manual/reference/bson-types/>)
or if the resulting document would be larger than 16MB you can use GridFS (see
this FAQ<http://docs.mongodb.org/manual/faq/developers/#faq-developers-when-to-use-gridfs>
).
Which language and driver are you using to add this data from your
application ? I can give a better answer regarding how you might do this
with this information.
Thanks!
Eoin
Post by Navneet Mathpal
Hi,
I am new in mongodb i have just started it.
I have just installed mongodb in my system,i want to insert a blob data
in mongodb how can i do it .
Thanks
Navneet Mathpal
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb

---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Navneet Mathpal
2014-02-12 14:25:33 UTC
Permalink
I am doing the follwing code with mongodb-java plugin and image is not
getting saved in data base ??




package test;

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("D:/Hydrangeas.jpg");
//Connect to database
Mongo mongo = new Mongo("127.0.0.1");
String dbName = "newdb";
DB db = mongo.getDB(dbName);
//Create GridFS object
GridFS fs = new GridFS( db );
//Save image into database
GridFSInputFile in = fs.createFile(imageBytes);
in.save();

Object abc= in.getId();
System.out.println(abc);
System.out.println(imageBytes);
System.out.println("file has been saved");

//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("D:/HydrangeasCOPY522267.jpg");
out.writeTo( outputImage );
outputImage.close();
}
}
Post by Navneet Mathpal
hi Eoin
I am using java and no idea about driver because i have just installed
mongodb, can u suggest..
Thanks
Navneet
Post by Eoin Brazil
Hi Navneet,
It depends on the size of your blob of data, you can store it in a
document using the binary data type (see this page for more info<http://docs.mongodb.org/manual/reference/bson-types/>)
or if the resulting document would be larger than 16MB you can use GridFS (see
this FAQ<http://docs.mongodb.org/manual/faq/developers/#faq-developers-when-to-use-gridfs>
).
Which language and driver are you using to add this data from your
application ? I can give a better answer regarding how you might do this
with this information.
Thanks!
Eoin
Post by Navneet Mathpal
Hi,
I am new in mongodb i have just started it.
I have just installed mongodb in my system,i want to insert a blob data
in mongodb how can i do it .
Thanks
Navneet Mathpal
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb

---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Jeremy Mikola
2014-02-13 21:00:32 UTC
Permalink
Post by Navneet Mathpal
hi Eoin
I am using java and no idea about driver because i have just installed
mongodb, can u suggest..
Using the Java driver, you can simply store a byte array and the driver
will convert it to the BSON binary type. This is discussed a bit in the Java
Types <http://docs.mongodb.org/ecosystem/drivers/java-types/#binary-data> documentation,
as well as this mailing list thread<https://groups.google.com/d/msg/mongodb-user/3qSHLSV7byM/X9Fm5QMIFEEJ>.
As Eoin mentioned above, if you are storing large binary blobs (exceeding
the 16MB document limit), you will need to look into using the GridFS API<http://docs.mongodb.org/manual/core/gridfs/>,
which is documented here<http://api.mongodb.org/java/current/com/mongodb/gridfs/GridFS.html> for
the Java driver.
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb

---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Loading...