Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6814177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:39:30+00:00 2026-05-26T20:39:30+00:00

How do I get a binary field from an existing mongo db document? At

  • 0

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')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:39:31+00:00Added an answer on May 26, 2026 at 8:39 pm

    As it turns out for: myDoc.binaryField.value( true ); The first 4 bytes (32-bits) are the big endian length of the size of the data. If you read from past those 4 bytes the buffer then that will end up being the data.

    In my case the start of the data looked like this in hex:

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how can I get 16 bytes binary form of the uuid from its string/canonical
I try to get TO: user@mail.com field from mime mail message. I have code:
If I set pre-compile script into binary code to true I get error saying
Is there anyway to take a binary - even signed by yourself, and get
We get a large amount of data from our clients in pdf files in
I get a URL from a user. I need to know: a) is the
I've got some binary data that I store and was going to separate this
This is what reflector gives: public int Int1 { get; set; } public string
I am trying to get RTF data out of a image date field in
I am getting some weird behaviour from protobuf binary file io. I am pre-processing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.