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 typesNumber
Double1
String2
Object3
Array4
Binary data5
Undefined6
Object Id7
Boolean9
Date10
Null11
Regular Expression12
JavaScript13
symbol14
JavaScript with scope15
Integer 16 and 18
timestamp10
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.
Example-

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

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

MongoDB Data Types

Binary Data – Data Types in MongoDB

f. Undefined

This MongoDB data type stores the undefined values.
Example-

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

Example-

MongoDB Data Types

ObjectId – Data Types in MongoDB

h. Boolean

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

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.

Example-

MongoDB Data Types

Date- Data Types in MongoDB

j. Null

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

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

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

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

MongoDB Data Types

Symbol – Data Types in MongoDB

n. JavaScript with Scope

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

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

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

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

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.

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

courses

DataFlair Team

The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.

1 Response

  1. Sunil. R says:

    Superb key points

Leave a Reply

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