2

I don't need gridFs as my images file won't be more than 16mb for sure. I also don't need file storage like s3 which I used for most of the project.

I have confusion with using bson data to directly store the image to mongodb document. Says I don't have a file from client, I have just an image link like example.com/v1/myImg.jpge, what should I do first? convert the images to base64 and then store it as binary type?

1
  • A base64 would be a string, but yes, that is the way to go. Commented Mar 16, 2016 at 12:28

1 Answer 1

4

You should probably fetch the whole file, and then either convert them to base64 and store it in a string, or, don't convert it to base64 and store it as Binary. The latter uses less space. You can wrap your data directly in Binary() - without the base64 conversion. See the docs at https://mongodb.github.io/node-mongodb-native/api-bson-generated/binary.html

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

2 Comments

what is the different btw base64 and binary? I thought image is best always in base64? I always use base64 image when i was using PHP.
base64 encodes binary data as a string. For every 3 original bytes, it needs four base64 characters, increasing the size by ⅓rd. Some languages/databases aren't great with binary data in specific contexts, and there base64 encoding makes sense. It's not needed for MongoDB (as long as you wrap it in Binary()).

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.