It stores documents on disk in the BSON serialization format. BSON is a binary representation of the JSON documents. Data format of BSON provides more data types than JSON. The mongo JavaScript shell and the MongoDB language drivers translate between the BSON format doucments and the language specificed document representation.
BSON supports the following data types as values in the documents. Each data type has a corresponding number (an integer ID number from 1 to 255) which can be used with the $type operator to query documents by the BSON type.
MongoDB Data Types and Corresponding ID Number
Type | Type | Number |
---|---|---|
Double | Represents a float value | 1 |
String | BSON strings are UTF-8. In general drivers for each programming language convert from the language’s string format to UTF-8 when serializing and deserializing BSON as this makes it possible to store most international characters in BSON strings with ease in addition MongoDB $regex queries support UTF-8 in the regex string | 2 |
Object | Represents an embedded documents | 3 |
Array | Sets or lists of values can be represented as arrays | 4 |
Binary data | Binary data is a string of arbitrary bytes it cannot be manipulated from the shell | 5 |
Object id | ObjectIds (MongoDB document identifier equivalent to a Primary key) are small likely unique fast to generate and ordered as these values consists of 12-bytes where the first four bytes are a timestamp that reflect the ObjectId’s creation | 7 |
Boolean | A logical true or false use to evaluate whether a condition is true or false | 8 |
Date | BSON Date is a 64-bit integer that represents the number of milliseconds since the Unix epoch (Jan 1 1970) as this results in a representable date range of about 290 million years into the past and future | 9 |
Null | It represents both a null value and a nonexistent field | 10 |
Regular Expression | RegExp maps directly to a Javascript RegExp | 11 |
JavaScript | 13 | |
Symbol | Not supported by the shell as if the shell gets a symbol from the database it will convert it into a string | 14 |
JavaScript (with scope) | 15 | |
32-bit integer | Numbers without decimal points will be saved as 32-bit integers. | 16 |
Timestamp | BSON has a special timestamp type for internal MongoDB use and is not associated with the regular Date type | 17 |
64-bit integer | Numbers without a decimal point will be saved and returned as 64-bit integers. | 18 |
Max key | MaxKey compare greater than all other possible BSON element values respectively and exist primarily for internal use | 127 |
Min key | MinKey compare lower than all other possible BSON element values respectively and exist primarily for internal use | 255 |
Different BSON types and their compairing values
When comparing the values of different BSON types, MongoDB uses the following comparison order, from lowest to highest:
Order | Data Types |
---|---|
1 | MinKey(internal type) |
2 | Null |
3 | Numbers(ints logs doubles) |
4 | Symbol and String |
5 | Object |
6 | Array |
7 | BinData |
8 | ObjectId |
9 | Boolean |
10 | Date and Timestamp |
11 | Regular Expressions |
12 | MaxKey(internal type) |
