Site icon DataFlair

MongoDB Data Types – 16 Various Data Types in MongoDB

MongoDB Data Types

MongoDB Data Types - 16 Various Data Types in MongoDB

FREE Online Courses: Click for Success, Learn for Free - Start 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-

Double – Data Types in MongoDB

In the above example, we have inserted a floating value 12345.67 in double data type after creating a variable.

b. String

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

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”.

String – Data Types in MongoDB

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

Object – Data Types in MongoDB

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.

Array – Data Types in MongoDB

e. Binary Data

These MongoDB data types store the binary data in it.
Example-

Binary Data – Data Types in MongoDB

f. Undefined

This MongoDB data type stores the undefined values.
Example-

Undefined – Data Types in MongoDB

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-

ObjectId – Data Types in MongoDB

h. Boolean

Boolean data type stores Boolean values i.e. true/false.
Example-

Boolean – Data Types in MongoDB

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-

Date- Data Types in MongoDB

j. Null

This MongoDB data types stores a null value in it.
Example-

Null – Data Types in MongoDB

k. Regular Expression

These MongoDB data types stores regular expressions in MongoDB. It maps directly to JavaScript RegExp.
Example-

Regular Expression – Data Types in MongoDB

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-

JavaScript – Data Types in MongoDB

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-

Symbol – Data Types in MongoDB

n. JavaScript with Scope

These MongoDB data types store JavaScript data with a scope.
Example-

Javascript with Scope – Data Types in MongoDB

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-

Integer – Data Types in MongoDB

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-

Timestamp – Data Types in MongoDB

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-

Min & Min Key – Data Types in MongoDB

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.

Exit mobile version