1. Objective: MongoDB Data Types
So, let’s discuss MongoDB Data Types.
2. MongoDB Data Types
|MongoDB Data types||Number|
|Integer||16 and 18|
3. Different Data Types in MongoDB
Here, we will discuss 16 various MongoDB Data Types with the examples, let’s discuss them one by one:
The double data type is used to store floating point values. Let’s see with an example, how we can do it.
In the above example, we have inserted a floating value 12345.67 in double data type after creating a variable.
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”.
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
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.
This MongoDB data type stores the undefined values.
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.
Boolean data type stores Boolean values i.e. true/false.
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()||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.|
This MongoDB data types stores a null value in it.
k. Regular Expression
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.
These MongoDB data types used to store an integer value. There are two forms available for this data type, 32 bits, and 64 bits.
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.
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.
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. In the next article, we will see how to insert, query, update and delete documents in MongoDB. Furthermore, if you have any query, feel free to ask in the comment section.