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.





















难得自然萌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...:)