(java) datatypes

167 views
Skip to first unread message

Scott Hernandez

unread,
Mar 26, 2010, 12:55:30 PM3/26/10
to mongodb-dev
I'm trying to figure out what data-types are supported in MongoDB.
Aside from taking apart the drivers to see what they do is there a
list someplace? I also want to find out the mapping of datatypes in
Java, and other languages. Is there a list for that too?

http://www.mongodb.org/display/DOCS/Java+Types
http://www.mongodb.org/display/DOCS/BSON
http://bsonspec.org/#/specification

So far testing seems to indicate this (with the 1.4 java driver):

java - mongodb
int -> int
long -> long
String -> String
boolean -> boolean
float -> double
double -> double
short -> double (huh?)
byte -> double (wha-what?)

http://www.idevelopment.info/data/Programming/java/miscellaneous_java/Java_Primitive_Types.html

The strange thing here is the short/byte java driver conversion. I
don't know about you, but I expected an int if short/byte wasn't
supported.

Matt Mastracci

unread,
Mar 26, 2010, 1:28:45 PM3/26/10
to mongo...@googlegroups.com
I'm not an expert in the driver code (only started using it recently), but I suspect the problem is in ByteEncoder's putNumber. The instanceof tests should also test Short and Byte (and Character?) in addition to integer:

protected int putNumber( String name , Number n ){
int start = _buf.position();
if ( n instanceof Integer ){
_put( NUMBER_INT , name );
_buf.putInt( n.intValue() );
}
else if (n instanceof Long ) {
_put( NUMBER_LONG , name );
_buf.putLong( n.longValue() );
}

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

Eliot Horowitz

unread,
Mar 26, 2010, 1:30:38 PM3/26/10
to mongo...@googlegroups.com
i'm not actually sure what the best thing to do for short/byte types.

It might be better to throw an exception rather than convert silently...

Definitely agree double is wrong though.

Whatever people seem to want I can do.

Scott Hernandez

unread,
Mar 26, 2010, 1:49:23 PM3/26/10
to mongo...@googlegroups.com
It doesn't seem like silently converting is a problem, if you can do
it both in and back out. The problem is that it is changing the
datatype silently in a one-way mapping.

My bigger question is, what are the supported datatypes? It is hard to
suggest behavior without knowing what the options are :)

Michael Dirolf

unread,
Mar 26, 2010, 1:53:47 PM3/26/10
to mongo...@googlegroups.com
MongoDB uses BSON v1 - so the supported datatypes are those listed here:
http://bsonspec.org/#/specification

Matt Mastracci

unread,
Mar 26, 2010, 2:00:15 PM3/26/10
to mongo...@googlegroups.com
Here's what I reverse engineered from the source. There's already a lot of auto-conversion magic happening (byte[] roundtrips to DBBinary, List<>/Array<> roundtrip to BasicDBList), so I don't think auto-converting byte/short to NUMBER_INT will be harmful. I'd also recommend doing something with char - either STRING or NUMBER_INT.

BSON Java

NULL null
UNDEFINED null
BOOLEAN Boolean
NUMBER Double
NUMBER_INT Integer
NUMBER_LONG Long
SYMBOL String
STRING String
OID mongodb ObjectID
REF DBPointer
DATE Date
REGEX Pattern
BINARY DBBinary
CODE (exception)
ARRAY DBList
OBJECT DBObject or DBRef
TIMESTAMP DBTimestamp
MINKEY String: "MinKey"
MAXKEY String: "MaxKey"

Java to BSON is similar (tests are done via instanceof, from top to bottom):

Java BSON

null NULL
Date DATE
Integer NUMBER_INT
Long NUMBER_LONG
Number NUMBER
String STRING
ObjectID OID
DBObject OBJECT
Boolean BOOLEAN
Pattern PATTERN
DBRegEx PATTERN (deprecated)
Map OBJECT
List ARRAY
byte[] BINARY
DBBinary BINARY
generic[] ARRAY
DBPointer REF (deprecated)
DBRefBase OBJECT
DBSymbol SYMBOL
DBUndefined UNDEFINED
DBTimestamp TIMESTAMP

Eliot Horowitz

unread,
Mar 26, 2010, 2:06:40 PM3/26/10
to mongo...@googlegroups.com
Ok - changed Short and Byte to be INTEGERs

Not sure about Character.
My inclination right now is to leave the current behavior (throwing exception).
I don't love the idea of converting to String or Integer

Scott Hernandez

unread,
Mar 26, 2010, 2:10:30 PM3/26/10
to mongo...@googlegroups.com
Cool, thanks for pulling that out, Matt.

Having the system map byte -> double seemed very confusing; esp since
there seems to be a byte (8bit) datatype in BSON (or have I misread
the spec?).

I'd say char -> String is a better mapping, but less space efficient.

Eliot, sounds good with short<->int.

On Fri, Mar 26, 2010 at 11:06 AM, Eliot Horowitz

Michael Dirolf

unread,
Mar 26, 2010, 2:12:27 PM3/26/10
to mongo...@googlegroups.com
Nope, no byte type in BSON - the types are the things listed down the
right side in the "element" section of the BNF.

Eliot Horowitz

unread,
Mar 26, 2010, 2:13:13 PM3/26/10
to mongo...@googlegroups.com
There is a byte construct, but not a type.

There is a bool type, but that wouldn't work.

Not sure why - but char -> String seems wrong to me... Not sure why

On Fri, Mar 26, 2010 at 2:10 PM, Scott Hernandez
<scotthe...@gmail.com> wrote:

Scott Hernandez

unread,
Mar 26, 2010, 2:21:18 PM3/26/10
to mongo...@googlegroups.com
I hear you. I would naturally say int; not sure why I went with String.

Maybe you are correct, and we should leave char off the conversion list

On Fri, Mar 26, 2010 at 11:13 AM, Eliot Horowitz

Matt Mastracci

unread,
Mar 26, 2010, 2:23:43 PM3/26/10
to mongo...@googlegroups.com
char is always ambiguous in Java - you're never sure if it's being used as an unsigned short or a real character. I wouldn't be surprised if it threw an exception or was auto-converted to string, but I'd probably be surprised if it was auto-converted to an integer.

I can't even think of a strawman example where I'd want to store a char in the database where I wouldn't want to just specify a string instead.

I'd vote for explicitly throwing an exception in this case.

Matt.

Scott Hernandez

unread,
Mar 26, 2010, 4:59:17 PM3/26/10
to mongodb-dev
I changed Morphia to store it as a String. Also, based on the java
field type we are mapping to, it will grab the first character and
return that. In that respect maybe this is best left as an exception
in the driver.

If we had to choose a mapping in the driver, I would go with String
because it is easier to read when looking at the output from on the
screen. I know what "M" looks like but it would take me a while to
lookup "M"'s ansi/unicode number (and that is depending on the
charset too).

On Mar 26, 11:23 am, Matt Mastracci <matt...@mastracci.com> wrote:
> char is always ambiguous in Java - you're never sure if it's being used as an unsigned short or a real character. I wouldn't be surprised if it threw an exception or was auto-converted to string, but I'd probably be surprised if it was auto-converted to an integer.
>
> I can't even think of a strawman example where I'd want to store a char in the database where I wouldn't want to just specify a string instead.
>
> I'd vote for explicitly throwing an exception in this case.
>
> Matt.
>
> On 2010-03-26, at 12:13 PM, Eliot Horowitz wrote:
>
>
>
> > There is a byte construct, but not a type.
>
> > There is a bool type, but that wouldn't work.
>
> > Not sure why - but char -> String seems wrong to me...  Not sure why
>
> > On Fri, Mar 26, 2010 at 2:10 PM, Scott Hernandez
> > <scotthernan...@gmail.com> wrote:
> >> Cool, thanks for pulling that out, Matt.
>
> >> Having the system map byte -> double seemed very confusing; esp since
> >> there seems to be a byte (8bit) datatype in BSON (or have I misread
> >> the spec?).
>
> >> I'd say char -> String is a better mapping, but less space efficient.
>
> >> Eliot, sounds good with short<->int.
>
> >> On Fri, Mar 26, 2010 at 11:06 AM, Eliot Horowitz
> >> <eliothorow...@gmail.com> wrote:
> >>> Ok - changed Short and Byte to be INTEGERs
>
> >>> Not sure about Character.
> >>> My inclination right now is to leave the current behavior (throwing exception).
> >>> I don't love the idea of converting to String or Integer
>

> >>>>>>>>http://www.idevelopment.info/data/Programming/java/miscellaneous_java...


>
> >>>>>>>> The strange thing here is the short/byte java driver conversion. I
> >>>>>>>> don't know about you, but I expected an int if short/byte wasn't
> >>>>>>>> supported.
>
> >>>>>>>> --

> >>>>>>>> You received this message because you are subscribed to the Google Groups "mongodb-dev" group.>>>>>>>> To post to this group, send email tomong...@googlegroups.com.>>>>>>>> To unsubscribe from this group, send email tomongodb-de...@googlegroups.com.
> >>>>>>>> For more options, visit this group athttp://groups.google.com/group/mongodb-dev?hl=en.
>
> >>>>>>> --
> >>>>>>> You received this message because you are subscribed to the Google Groups "mongodb-dev" group.>>>>>>> To post to this group, send email tomong...@googlegroups.com.>>>>>>> To unsubscribe from this group, send email tomongodb-de...@googlegroups.com.
> >>>>>>> For more options, visit this group athttp://groups.google.com/group/mongodb-dev?hl=en.
>
> >>>>>> --
> >>>>>> You received this message because you are subscribed to the Google Groups "mongodb-dev" group.>>>>>> To post to this group, send email tomong...@googlegroups.com.>>>>>> To unsubscribe from this group, send email tomongodb-de...@googlegroups.com.
> >>>>>> For more options, visit this group athttp://groups.google.com/group/mongodb-dev?hl=en.
>
> >>>>> --
> >>>>> You received this message because you are subscribed to the Google Groups "mongodb-dev" group.>>>>> To post to this group, send email tomong...@googlegroups.com.>>>>> To unsubscribe from this group, send email tomongodb-de...@googlegroups.com.
> >>>>> For more options, visit this group athttp://groups.google.com/group/mongodb-dev?hl=en.
>
> >>>> --
> >>>> You received this message because you are subscribed to the Google Groups "mongodb-dev" group.>>>> To post to this group, send email tomong...@googlegroups.com.>>>> To unsubscribe from this group, send email tomongodb-de...@googlegroups.com.
> >>>> For more options, visit this group athttp://groups.google.com/group/mongodb-dev?hl=en.
>
> >>> --
> >>> You received this message because you are subscribed to the Google Groups "mongodb-dev" group.>>> To post to this group, send email tomong...@googlegroups.com.>>> To unsubscribe from this group, send email tomongodb-de...@googlegroups.com.
> >>> For more options, visit this group athttp://groups.google.com/group/mongodb-dev?hl=en.
>
> >> --
> >> You received this message because you are subscribed to the Google Groups "mongodb-dev" group.>> To post to this group, send email tomong...@googlegroups.com.>> To unsubscribe from this group, send email tomongodb-de...@googlegroups.com.
> >> For more options, visit this group athttp://groups.google.com/group/mongodb-dev?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups "mongodb-dev" group.> To post to this group, send email tomong...@googlegroups.com.> To unsubscribe from this group, send email tomongodb-de...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages