MongoDB Data Types – 16 Various Data Types in MongoDB

Job-ready Online Courses: Click for Success - 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 typesNumber
Binary data5
Object Id7
Regular Expression12
JavaScript with scope15
Integer 16 and 18
Min key255
Max key127

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.

MongoDB Data Types

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

MongoDB Data Types

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

MongoDB Data Types

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.

MongoDB Data Types

Array – Data Types in MongoDB

e. Binary Data

These MongoDB data types store the binary data in it.

MongoDB Data Types

Binary Data – Data Types in MongoDB

f. Undefined

This MongoDB data type stores the undefined values.

Undefined - Data Types in MongoDB

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)
Machine Id3
Process Id2


MongoDB Data Types

ObjectId – Data Types in MongoDB

h. Boolean

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

MongoDB Data Types

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


MongoDB Data Types

Date- Data Types in MongoDB

j. Null

This MongoDB data types stores a null value in it.

MongoDB Data Types

Null – Data Types in MongoDB

k. Regular Expression

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

MongoDB Data Types

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.

MongoDB Data Types

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.

MongoDB Data Types

Symbol – Data Types in MongoDB

n. JavaScript with Scope

These MongoDB data types store JavaScript data with a scope.

MongoDB Data Types

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.

MongoDB Data Types

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.

MongoDB Data Types

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.

MongoDB Data Types

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.


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 this article? If Yes, please give DataFlair 5 Stars on Google


1 Response

  1. Sunil. R says:

    Superb key points

Leave a Reply

Your email address will not be published. Required fields are marked *