Store binary data with Java driver

349 views
Skip to first unread message

Javier

unread,
May 28, 2011, 9:09:12 AM5/28/11
to mongodb-user
What is the correct way if storing binary data using Java driver?

If I put a byte[] in a DBObject, the data is encoded in the depecated
way (the one with subtype 2)
If I put a Binary with subtype 0, the data is encoded correctly but it
is decoded to a byte[] instead of a Binary.
If I put a Binary with user-defined subtype (80), the data is encoded
correctly and decoded to a Binary object.

The third one seems the way to go, but I'm not sure if setting user-
defined subtype for something that is general binary data is correct.
Any hints?

Many thanks

Scott Hernandez

unread,
May 28, 2011, 10:31:37 AM5/28/11
to mongod...@googlegroups.com
What is wrong with just using a byte[] ? If you just want to store binary data that should work just fine.

The 2.6 driver will store with subtype 0 by default; which is the new standard across all drivers. Both subtype 0 and 2 will be returned as a byte[].


--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Javier

unread,
May 29, 2011, 10:25:38 AM5/29/11
to mongodb-user

The problem I see with using a byte[] is that it is encoded in the
deprecated way (subtype 2) that stores the data lenght twice, so there
is an overhead of 4 bytes per value.

Scott Hernandez

unread,
May 29, 2011, 5:09:11 PM5/29/11
to mongod...@googlegroups.com
Yes, in the 2.6.x driver the default has changed (to subtype 0, without that overhead).
Reply all
Reply to author
Forward
0 new messages