Welcome to a tutorial on Datatypes in MongoDB.
As you may already know, MongoDB stores the JSON data in a binary encoded format called BSON, where the JSON data model actually extends the BSON to provide more flexibility with the additional data types and helps in easily encoding and decoding the data across different languages. However, by default, there is no support for the datatypes like date in JSON, but BSON provides that feature.
MongoDB supports the list of datatypes in the table. Every datatype in MongoDB possesses a unique number.
Datatype | Number | Description |
Double | 1 | It is used to store floating point values |
String | 2 | It is a commonly used datatype and it is UTF-8 valid |
Object | 3 | It is used for storing embedded objects |
Array | 4 | It is used for storing embedded objects |
Binary Data | 5 | It is used to store binary data |
Undefined | 6 | It is used to store undefined value |
Object Id | 7 | It is used to store a document's ID |
Boolean | 9 | It is used to store a Boolean value |
Date | 10 | It is used to store the current date time in UNIX format. |
Null | 11 | It is used to store a null value |
Regular Expression | 12 | It is used to store the regex |
Javascript | 13 | It is used to store JavaScript data without scope |
Symbol | 14 | Typically, it is used to store strings, but reserved for languages that use a specific symbol |
Javascript with scope | 15 | It is used to store JavaScript data with a scope |
Integer | 16 & 18 | It is used to store a numerical value |
Timestamp | 10 | It is used to track when a document is modified. |
Min/Max Key | 255/127 | It is used to compare a value against the lowest and highest BSON elements |