the binary insert is done in java so here is the insert code:
Mongo mongo = new Mongo("localhost");
DB db = mongo.getDB("mydb");
DBCollection collection = db.getCollection("myfiles");
String filename = "localfile.jpg";
File file = new File(filename);
FileInputStream is = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
is.read(data);
is.close();
BasicDBObject myfile = new BasicDBObject("filename", filename).append
("data", data);
collection.insert(myfile);
lets assume that the file localfile.jpg has a size of 13824 bytes then
in the mongo shell the output would look like:
{ "_id" : ObjectId("3dec215d9062e04a6e98d700"), "filename" :
"localfile.jpg", "data" : BinData type: 2 len: 13828 }
so there is a 4 byte difference which is the size of one integer.
the php code to read the data looks like this:
$mongo = new Mongo();
$db = $mongo->selectDB('mydb');
$collection = $db->selectCollection('myfiles');
$query = array('_id' => new MongoId('3dec215d9062e04a6e98d700'));
$myfile = $collection->findOne($query);
file_put_contents('binary.tmp', $myfile['data']->bin);
file_put_contents('substr.tmp', substr($myfile['data']->bin, 4));
the file binary.tmp will have a size of 13828 bytes and the file
substr.tmp will have a size of 13824 and binary equals the original
file localfile.jpg
On 27 Okt., 18:40, Kristina Chodorow <
krist...@10gen.com> wrote:
> I tried to reproduce what you're seeing with plain MongoBinData and GridFS
> and couldn't reproduce it with either. As Eliot said, it would be really
> helpful to have an example.
>