Getting a binary field from a MongoDB driver

How to get binary field from existing mongo db doc?

In the MongoDB console, if I find a record for the selection, I get the following:

{_id:ObjectId("1234"),"cover_data" : BinData(2,"ozkAAP/Y/+AAEEpGSUYAAQEBAJYAlgAA/+IFpElDQ19QUk9GSUxFAAEBAAAFlGFwcGwCIAAAbW50clJHQiBYWVogB9kAAgAZAAsAGgALYWNzcEFQUEwAAAAAYXBwbAAAAAAAAAAAAAAAAAAAAAAAAPbWAAEAAAA ..... ) 

In python on our web server, when we find with pymongo, it receives this field as binary and json_pickle seems to automatically turn it into base64 and, alas, the image looks great when sent back to the client. When I compare the generated base64 with the node.js mongo driver, it is completely different and does not display the image correctly.

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 do not see any example of what I'm trying to do. It seems that there are BSON and BinaryParser deserializers that are used with the whole BSON object. I tried this for only one field and got segmentation errors.

Running the list of things I 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') 
+4
source share
2 answers

As it turns out: myDoc.binaryField.value( true ); The first 4 bytes (32 bits) is the large final length of the data size. If you read from the past these 4 bytes a buffer that will eventually become data.

In my case, the beginning of the data in hexadecimal form was as follows:

63 12 00 00 ff d8 ff e0 00 10 4a 46

The python data looked like this:

ff d8 ff e0 00 10 4a 46

 Binary = client.bson_serializer.Binary binary = new Binary(myDoc.binaryField.value( true )) buffer = new Buffer(binary.toString(),'binary') length_buf = buffer.slice(0,4) length = length_buf[3] << 32 | length_buf[2] << 16 | length_buf[1] << 8 | length_buf[0] buffer.slice(4).slice(0,length).toString(enc) 
+2
source

I am a little confused by what you are trying to do. When selecting / pasting binary elements in MongoDB using node-Mongodb you should use the bson binary.

 var buffer = new Buffer(); /* your binary data */ var binary = client.bson_serializer.Binary( buffer ); var myDoc = { binaryField: binary; } //And for when selecting the document var buffer = myDoc.binaryField.value( true ); 

Set the toString argument to true to select it as a buffer; false to return as โ€œbinary,โ€ but as the manual says, it should be avoided with respect to the buffer object.

To translate from the buffer to base64:

 buffer.toString('base64'); 
+4
source

Source: https://habr.com/ru/post/1380096/


All Articles