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

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

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.
you want to do the inserts from the mongo client? why?
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
-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.

2 Comments

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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.