The Top 12 BSON Data Types you won't find in JSON

Facebook ShareLinkedin ShareReddit ShareTwitter Share

People call MongoDB a JSON database but MongoDB actually uses BSON, a binary version of JSON. One of the big things with BSON is that it has so many more data types than JSON which means you can query things with a lot more precisions. How many data types? Let's run down this list of data types you won't find in JSON….

There are two BSON data types that didn't make the list: Boolean and Null. Boolean maps to two JSON values, true and false, so you will find it in JSON. Null is also a value in JSON, but in BSON it turns into a type.

12: Minkey and 11: Maxkey: Although these are BSON data types, they exist only to represent the extremes of keys - the minimum key value and the maximum key value. Why? So you can write a query as a document that describes a range of keys that starts from the smallest key or ends with the largest key.

10: Binary Data: Also known as BinData, this BSON data type is for arrays of bytes because representing bit arrays efficiently is important when you're storing and searching data.

9: TimeStamp: It's a type but for representing time, but it's mostly for internal use. If you want to be storing dates and times, you want...

8: Date: This is actually a date and time as an unsigned 64-bit integer with a UTC (Universal Time Coordinates) time zone.

7: ObjectID: Part of the magic of MongoDB, the ObjectID is like a UUID but smaller. MongoDB generates them to ensure documents are uniquely identified. It's actually 12 bytes in size and gets filled in with 4 bytes for seconds since the epoch, 5 bytes of just randomness and 3 bytes from a randomly incremented counter.

6: Regular Expression: The handy pattern-matching power of regular expression strings is used so often that we thought it needed its own type to save on that "convert from string" step. This comes into its own when you are writing database objects with validation patterns or matching triggers.

5: JavaScript: Like regular expressions, JavaScript functions can be stored in BSON as their own type. For MongoDB applications, this is generally used for stowing away snippets of code to be run in the client application. It's really not a good idea to run the snippets in the server.

4: Double: JSON calls anything with numbers a Number. That leaves it up to implementations to figure out how to turn it into the nearest native data type. In BSON, Double is the default replacement for JSON's Number and that means we can have more specialized number types like...

3: Int: 32 bits of integer precision which, for when you have small integer values and you don't want to store it as a sequence of digits. Simple, precise and when you run out of 32-bit integer, there's always...

2: Long: 64 bits of integer precision, for when you have bigger integer values and you really don't want to store it as a sequence of digits. Simple, precise and when you really want huge numbers with lots of floating point range, you can move up to the star of the type-show…

1: Decimal128: As the name says, it is 128 bits of high precision decimal representation. 34 decimal digits of precision, with a max value of around 10^6145 and a minimum of, you may have guessed it -10^6145. This is the IEEE-754-2008 128-bit decimal floating point number, for when you absolutely have to store huge, or tiny numbers.

Oh, do note that you'll have to be prepared to convert Decimal128 into your favorite language's bigdecimal library because no one comes equipped to handle these huge numbers. You'll want MongoDB 3.4 or later to enjoy our top BSON data type, but who doesn't want huge number support?

So, that's our top 12 BSON data types. If we missed your favorite or you've got your own ranking, let us know in the comments!