MongoDB Data Types (Day 5)

Introduction

 
Before reading this article, I highly recommend reading the previous installments in the series: 
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
 
Double
 
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
 
String
 
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
 
Object
 
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
 
Array
 
Binary Data
 
The Binary data type stores binary data.
 
Example
 
Binary Data
 
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
 
ObjectId
 
Boolean
 
This type stores a boolean value. This data type can be set to either true or false.
 
Example
 
Boolean
 
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
 
Date
 
Null
 
The Null data type stores a Null value.
 
Example
 
Null
 
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
 
Integer
 
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
 
starting value of Timestamp
 
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.
 
insert some document
 
Now, we update a record and once again check the value of the Timestamp.
 
Update some record
 
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:
  1. MinKey
  2. Null
  3. Numbers (ints, longs, doubles)
  4. Symbol, String
  5. Object
  6. Array
  7. BinData
  8. ObjectId
  9. Boolean
  10. Date
  11. Timestamp
  12. Regular Expression
  13. MaxKey
Example:
 
Max keys
 
Now we insert another document into the collection, then sort the result and examine the output:
 
document into collection
 
Regular Expression
 
The Regular Expression data type stores regular expressions.
 
Example
 
Regular Expression
 
Undefined
 
The Undefined data type stores an undefined value.
 
Example
 
Undefined
 
Symbol
 
The Symbol data type is similar to the string data type. Symbol is mainly reserved for languages that use specific symbols.
 
Example
 
Symbol
 
JavaScript
 
The JavaScript data type stores JavaScript data without Scope.
 
Example
 
JavaScrip
 
JavaScript with Scope
 
This data type stores JavaScript data with a scope option.
 
Example
 
JavaScript with Scope
 
This article explained the basic data types of MongoDB. In the next article, we will learn about CRUD operations.