Introduction
Before reading this article, I highly recommend reading the previous installments in the series:
- MongoDB - Day 1 (Introduction To MongoDB)
- MongoDB - Day 2 (Install MongoDB in Windows)
- MongoDB - Day 3 (Database Basics)
- MongoDB - Day 4 (Basics of Collection)
Internally, MongoDB stores JSON documents in a binary-encoded format called BSON. BSON extends the JSON model to provide additional data types and to be efficient for encoding and decoding within various languages. It adds support for data types, such as date and binary, which aren't supported in JSON. BSON is a binary serialization format for storing documents and to make Remote Procedure Calls in MongoDB.
MongoDB supports the following data types. Each data type has a corresponding number. The $type method identifies a data type using its corresponding number.
| Data Type | Number |
| Double | 1 |
| String | 2 |
| Object | 3 |
| Array | 4 |
| Binary Data | 5 |
| Undefined | 6 |
| Object Id | 7 |
| Boolean | 9 |
| Date | 10 |
| Null | 11 |
| Regular Expression | 12 |
| JavaScript | 13 |
| Symbol | 14 |
| JavaScript with scope | 15 |
| Integer | 16 and 18 |
| Timestamp | 10 |
| Min Key | 255 |
| Max Key | 127 |
Now, we will learn some basic data types with examples.
Double
The Double data type stores floating-point values.
Example
In the above example, we created a variable (double_) of the floating type, inserted a float type value into this variable, and finally printed the value of this variable.
String
String is the most commonly used data type. It contains a string of text or any other type of character. BSON strings are UTF-8. In general, drivers for each programming language convert from the language string format to UTF-8 when serializing and deserializing BSON. If a string is not valid, it will not be saved. This makes it possible to store most international characters in BSON strings.
Example
Object
This data type stores embedded documents. When a document contains another document in the form of key-value pair then such type of document is known as an embedded document.
Example
Array
The Array data type stores arrays. An Array type can store multiple values of multiple data types (Integer, Double, Date, String, and so on).
Example
Binary Data
The Binary data type stores binary data.
Example
In the preceding example, BinData is the base64 representation of a binary string.
ObjectId
The ObjectId data type stores the document's ID. ObjectId is small, likely unique, fast to generate, and ordered. The size of ObjectId is 12 bytes. These 12 bytes are divided into the following 4 parts.





| Part Name | Size(Bytes) |
| Time Stamp | 4 |
| Machine Id | 3 |
| Process Id | 2 |
| Counter | 3 |
MongoDB uses an _id field for each document to uniquely identify them. This _id field s a primary something. It can't be a duplicate. Data is stored in hexadecimal format in the _id field.
Example
Boolean
This type stores a boolean value. This data type can be set to either true or false.
Example
Date
This data type stores the current date or time in UNIX time format. MongoDB provides various methods to return the date, either as a string or as a Date object.


| Date Method | Description |
| Date() | returns the current date as a string. |
| new Date() | returns a Date object using the ISODate() wrapper. |
| ISODate() | returns a Date object using the ISODate() wrapper. |
We can specify your own date time by creating an object of Date and passing a day, month, and year into it. Date objects are stored as a 64-bit integer representing the number of milliseconds.
Example
Null
The Null data type stores a Null value.
Example
Integer
The Integer data type stores an integer (Numeric) value. Integer data types are available in two forms 32 bits and 64 bits. An Integer can be 32 bits or 64 bits depending upon the server.
Example
Timestamp
The Timestamp data type stores a timestamp. This can be useful for recording when a document has been modified or added. Timestamp values are a 64-bit value.



- The first 32 bits are a time_t value (seconds since the Unix epoch)
- The second 32 bits are an incrementing ordinal for operations within a given second.
Let's see an example.
Example 1
In the preceding example, we can see that the starting value of the Timestamp is (0,0). After the insertion operation, the value of the timestamp is (1438791098,1). The first value of the timestamp is the current timestamp and the second value is the order of operation.
Example 2
MongoDB stores the current timestamp in ObjectId but we can retrieve this timestamp using the getTimestamp() method.
First, we insert a document into the collection, then we check the value of the Timestamp.
Now, we update a record and once again check the value of the Timestamp.
Min/Max keys
Min/Max keys compare a value against the lowest and highest BSON elements. Min and Max keys both are Internal data types. When comparing values of different BSON types, MongoDB uses the following comparison order from lowest to highest:



- MinKey
- Null
- Numbers (ints, longs, doubles)
- Symbol, String
- Object
- Array
- BinData
- ObjectId
- Boolean
- Date
- Timestamp
- Regular Expression
- MaxKey
Example:
Now we insert another document into the collection, then sort the result and examine the output:
Regular Expression
The Regular Expression data type stores regular expressions.
Example
Undefined
The Undefined data type stores an undefined value.
Example
Symbol
The Symbol data type is similar to the string data type. Symbol is mainly reserved for languages that use specific symbols.
Example
JavaScript
The JavaScript data type stores JavaScript data without Scope.
Example
JavaScript with Scope
This data type stores JavaScript data with a scope option.
Example
This article explained the basic data types of MongoDB. In the next article, we will learn about CRUD operations.







Next article >> MongoDB Insert Method (Day 6)

难得自然萌Posted Dec 23, 2020, 12:42 AM
Thanks for your article~
Hieu NguyenPosted Jul 2, 2019, 4:05 AM
Thank you so much, some day ago, I have just learn more about MongoDb. Hope to learn more about your technical topics.
Sr KarthigaPosted Feb 14, 2016, 7:41 AM
Good one sir
Rupali ShindePosted Oct 1, 2015, 7:34 AM
Good Article
Pankaj Kumar ChoudharyPosted Aug 7, 2015, 6:58 AM
Thanks Ankit Bansal Sir.........
Pankaj Kumar ChoudharyPosted Aug 7, 2015, 6:58 AM
Thanks Yashwant Sir........
Ankit BansalPosted Aug 7, 2015, 2:11 AM
nice...thanks for share..
Yashwant VishwakarmaPosted Aug 7, 2015, 1:22 AM
Truly Wao... Keep it up !!!
Pankaj Kumar ChoudharyPosted Aug 6, 2015, 8:44 PM
Thanks Rakesh Chavda Sir..........
Pankaj Kumar ChoudharyPosted Aug 6, 2015, 8:44 PM
Thanks Santhakumar Munuswamy Sir.........
Pankaj Kumar ChoudharyPosted Aug 6, 2015, 8:43 PM
Thanks Gopi Chand Sir.......
Pankaj Kumar ChoudharyPosted Aug 6, 2015, 8:43 PM
Thanks Gowtham Rajamanickam Sir.......
Pankaj Kumar ChoudharyPosted Aug 6, 2015, 8:43 PM
thanks Sibeesh Venu Sir......
Santhakumar MunuswamyPosted Aug 6, 2015, 2:08 PM
Nice Article. Thanks for sharing
Gopi ChandPosted Aug 6, 2015, 12:33 PM
Keep it up...
RakeshPosted Aug 6, 2015, 10:34 AM
Good explain
Gowtham RajamanickamPosted Aug 6, 2015, 9:43 AM
good one..
Sibeesh VenuPosted Aug 6, 2015, 8:33 AM
Nice Share
Pankaj Kumar ChoudharyPosted Aug 6, 2015, 8:03 AM
Thanks Rahul Prajapat Sir.............
Pankaj Kumar ChoudharyPosted Aug 6, 2015, 8:03 AM
Thanks Nilesh Jadav Sir.......
Rahul PrajapatPosted Aug 6, 2015, 7:58 AM
Nice article pankaj sir
Nilesh JadavPosted Aug 6, 2015, 7:54 AM
Nice Pankaj Kumar Choudhary sir , Great Post
Pankaj Kumar ChoudharyPosted Aug 6, 2015, 5:02 AM
Thanks Amol Sarkate Sir........
Pankaj Kumar ChoudharyPosted Aug 6, 2015, 5:01 AM
Thanks Rajeesh Sir.........
Amol SarkatePosted Aug 6, 2015, 4:50 AM
nice article
Rajeesh MenothPosted Aug 6, 2015, 3:11 AM
Good Articles in a row...:)