MongoDB Data Types – 16 Various Data Types in MongoDB
Expert-led Courses: Transform Your Career – Enroll Now
In our last article, we have seen how to create and drop collections in MongoDB. We have seen the syntax and command to execute these operations. Now, before going further, we should know about MongoDB Data Types.
In this article, we are going to discuss the 16 different Data Types in MongoDB: MongoDB Double, MongoDB string, MongoDB object, MongoDB array, MongoDB javascript with scope, with their examples.
So, let’s discuss MongoDB Data Types.
MongoDB Data Types
MongoDB stores documents in BSON, which is the binary encoded format of JSON. Basically, the name BSON itself comes from Binary encoded JSON. The BSON data format provides various types, used when we store the JavaScript objects in the binary form.
We can make remote procedure calls in MongoDB by using BSON. All the BSON data-types are supported in MongoDB. Below are the enlisted MongoDB data types. Each MongoDB datatypes corresponds a unique number which is used to identify them in $type method.
MongoDB Data types | 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 |
Different Data Types in MongoDB
Here, we will discuss 16 various MongoDB Data Types with the examples, let’s discuss them one by one:
a. Double
The double data type is used to store floating point values. Let’s see with an example, how we can do it.
Example-
In the above example, we have inserted a floating value 12345.67 in double data type after creating a variable.
b. String
This is the most commonly used MongoDB data types, BSON strings are UTF-8. Drivers for each programming language convert from the string format of the language to UTF-8 while serializing and de-serializing BSON.
This makes possible to easily store most international characters in BSON strings. The string must be valid to be saved.
Example- We have inserted a string in a document. Here, we have used a database “dataflair”.
c. Object
Object data type stores embedded documents. If a document contains another document in the form of the key-value pair then such type of document is known as an embedded document.
Example- We have inserted a document named local in another document of database “dataflair”. This is stored in the object data type
d. Array
These MongoDB data types stores array. A set of values are represented as an array. This data type can store multiples of values and data types.
Example- Let’s store some values in variable arrays, local1, local2, local3. We have stored a string, Integer, float, and date data type in these array data type.
e. Binary Data
These MongoDB data types store the binary data in it.
Example-
f. Undefined
This MongoDB data type stores the undefined values.
Example-
g. ObjectId
This data type in MongoDB stores the unique key Id of documents stored. There is an_id field in MongoDB for each document. The data which is stored in Id is in hexadecimal format. The size of ObjectId is 12 bytes which are divided into four parts as follows.
Part name | Size(bytes) |
Timestamp | 4 |
Machine Id | 3 |
Process Id | 2 |
Counter | 3 |
Example-
h. Boolean
Boolean data type stores Boolean values i.e. true/false.
Example-
i. Date
Date data type stores current date or time. There are various methods to return date. It can be either as a string or as a date object. In the below table, we have discussed the methods for the date.
Date Method | Description |
Date() | It returns the current date in string format. |
New Date() | Returns a date object. Uses the ISODate() wrapper. |
ISODate() | It also returns a date object. Uses the ISODate() wrapper. |
Example-
j. Null
This MongoDB data types stores a null value in it.
Example-
k. Regular Expression
These MongoDB data types stores regular expressions in MongoDB. It maps directly to JavaScript RegExp.
Example-
l. JavaScript
These MongoDB data types store the JavaScript data without a scope. There is another data type to store data with a scope.
Example-
m. Symbol
These MongoDB data types similar to the string data type. It is not supported by a shell. But if the shell gets a symbol from the database, it is converted into strings.
Example-
n. JavaScript with Scope
These MongoDB data types store JavaScript data with a scope.
Example-
o. Integer
These MongoDB data types used to store an integer value. There are two forms available for this data type, 32 bits, and 64 bits.
Example-
p. Timestamp
This data type is used to store a timestamp. Also, this is useful when we modify our data to keep a record. This is 64-bit value data type.
Example-
In the given example the initial value of the timestamp is (0, 0), after that it is (1523790696, 1). Here the first value is the timestamp and another one is an order of operation.
q. Min & Max key
Min key compares the value of the lowest BSON element. Max key compares the value against the highest BSON element. Both are internal data types.
Example-
This was all about MongoDB Data Types Tutorial. Hope you like our explanation of different Data Types in MongoDB.
Conclusion
Hence, we have discussed the key MongoDB Data Types. BSON supports more data types then JSON. The time data-type is not supported in JSON, but BSON supports it.
Further, we will discuss how operations are performed on documents. Furthermore, if you have any query, feel free to ask in the comment section.
Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google
Superb key points