How do I get a binary field from an existing mongo db document?
At the MongoDB console, if I do a find for the record of choice I get this:
{_id:ObjectId("1234"),"cover_data" : BinData(2,"ozkAAP/Y/+AAEEpGSUYAAQEBAJYAlgAA/+IFpElDQ19QUk9GSUxFAAEBAAAFlGFwcGwCIAAAbW50clJHQiBYWVogB9kAAgAZAAsAGgALYWNzcEFQUEwAAAAAYXBwbAAAAAAAAAAAAAAAAAAAAAAAAPbWAAEAAAA ..... )
In python on our web server when we do a find with pymongo it gets that field as binary and json_pickle seems to turn it to base64 automatically and alas the image looks great when sent back to the client. When I compare the generated base64 to the node.js mongo driver it's completely different and doesn't display the image properly.
Here is the code for Node.JS:
cb = function(comp) {
thumb_buffer = new Buffer(comp.thumbnail_data.value(),'binary');
comp.thumbnail_data = thumb_buffer.toString('base64');
}
In the examples and test cases here: https://github.com/christkv/node-mongodb-native I don't see any example of what I'm trying to do. There seem to be BSON deserializers and a BinaryParser that is used in the case of a whole BSON object. I tried it for just the one field and got a segmentation faults.
Running list of things I've tried:
mongo_compositions.find {_id:{$in:ids}},{},(err,compositions) ->
for comp in compositions
do(comp) =>
thumb_buffer = comp.thumbnail_data.value(true)
test_buffer = Binary(thumb_buffer)
console.log test_buffer
console.log test_buffer.toString('base64')
#thumb_buffer = BSON.deserialize thumb_buffer
#thumb_buffer.write(comp.thumbnail_data.value(true))
#comp.thumbnail_data = thumb_buffer.toString('base64')
#cover_buffer = new Buffer(comp.cover_data.value(),'binary')
#console.log thumb_buffer.toString('base64')
#console.log "#{comp.composition_author} - #{comp.thumbnail_data.length}"
#comp.cover_data = cover_buffer.toString('base64')