3

Say I have a collection named Items. I'm trying to accomplish a document structure like this:

{
    "itemName": "Google Glass"
    "description": "Awesome Gadget"
    "*some_picture*": "*some_picture_object*"
}

The images that I want to store won't exceed the 16MB cap on BSON documents so I don't want to use GridFS. How can I accomplish the structure above? I'm new to mongoDB and am pretty lost

Fourth
  • 630
  • 1
  • 7
  • 22

2 Answers2

1

I'd use the BinData format for the field in your document that contains the image data. Exact usage varies depending upon language used.

For PHP, a sample code for your use case (store image file in collection) taken from the PHP manual http://www.php.net/manual/en/class.mongobindata.php :

<?php

$profile = array(
    "username" => "foobity",
    "pic" => new MongoBinData(file_get_contents("gravatar.jpg"), MongoBinData::GENERIC),
);

$users->save($profile);

?>

Perl http://api.mongodb.org/perl/current/MongoDB/DataTypes.html#Binary%20Data :

 # non-utf8 string
    my $string = "\xFF\xFE\xFF";

    $collection->insert({"photo" => \$string});

This previous answer has sample code to save an image using Python in MongoDB: saving picture to mongodb

Community
  • 1
  • 1
John Petrone
  • 26,943
  • 6
  • 63
  • 68
  • Thanks. I am actually working on the database manually using the mongo client on the console. I'm also using node.js and express but don't want to do any inserts from there. Is there a way to go about this? Thanks again. – Fourth May 20 '14 at 21:10
  • you want to do the inserts from the mongo client? why? – John Petrone May 20 '14 at 21:24
  • For now, just a separation of concerns. Makes things clearer for me as a beginner. I want the program to only read from the database – Fourth May 20 '14 at 21:40
-1

There shouldn't be anything stopping you (other then the cap on the bson document) from storing the bytes (string) of the image in "some_picture". When you select the document and grab the bytes, you treat it exactly as you would if you were reading the bytes from disk.

Consider though, that without using $project, when iterating over your collection, the image will have to be sent over the wire (an entire document is sent across the wire unless you are using aggregate pipeline or map/reduce)

This is as well as I can answer without knowing what language you are using in order to provide an example.

kwolfe
  • 1,663
  • 3
  • 17
  • 27
  • Thanks for the reply. I am using node.js, express and jade. But I am trying to figure out how to insert images into a document. For basic data types, its straightforward. `db.Items.insert({ "itemName" : "Google glass", "description" : "Simply Awesome" })` but what's the syntax for images? – Fourth May 20 '14 at 17:58
  • Its the same (if your not going to use gridfs). Your byte data is just a string, just like the rest of your example. You have to figure out how to convert your bytes back to a usable format when selecting it though. – kwolfe May 20 '14 at 18:21