Data Types in MongoDB (original) (raw)

Last Updated : 5 May, 2026

In MongoDB, data types specify the kind of values a field can store, such as strings, numbers, or dates. Selecting appropriate data types ensures efficient storage, accurate queries, and better performance.

Below are the most commonly used MongoDB data types.

1. Integer

In MongoDB, the integer data type is used to store an integer value. We can store integer data type in two forms 32-bit signed integer and 64-bit signed integer. These are used to store whole numbers, such as ages, counts, or any other numerical data that doesn't require decimal points.

**Example: We are storing the age of the student in the student collection:

2. Double

The double data type is used for storing floating-point numbers (decimal values). It's commonly used for storing data that requires decimal precision, such as prices, percentages, or scores.

**Example: We are storing the marks of the student in the student collection:

3. Boolean

The boolean data type stores one of two values: true or false. It's used for representing binary states, such as "active/inactive" or "pass/fail."

**Example: We are storing the final result of the student as pass or fail in boolean values.

4. Null

The null data type stores a null value. This is useful when you want to represent the absence of data, such as an optional field that may not be set.

**Example: The student does not have a mobile number so the number field contains the value null.

5. Array

The array data type allows us to store multiple values in a single field.MongoDB arrays can contain values of the same or different data types, providing flexibility in how you store collections of related data. In MongoDB, the array is created using square brackets([]).

**Example: We are storing the technical skills of the student as an array.

6. Object (Embedded Document)

Object data type stores embedded documents. Embedded documents are also known as nested documents. Embedded document or nested documents are those types of documents which contain a document inside another document. Embedded documents allow us to structure our data hierarchically, which is useful for representing more complex data models.

**Example: We are storing all the information about a book in an embedded document.

Object

7. ObjectId

Whenever we create a new document in the collection MongoDB automatically creates a unique objectId for that document(if the document does not have it). There is an _id field in MongoDB for each document. The data which is stored in Id is of hexadecimal format and the length of the id is 12 bytes which consist:

We can also create your own id field, but make sure that the value of that id field must be unique.

**Example: When we insert a new document it creates a new unique objectId for it.

Object Id

8. Undefined

The undefined data type represents a value that is not defined. It is rarely used in modern MongoDB applications, as it has been replaced by the null value for most practical purposes.

**Example: The type of the duration of the project is undefined.

9. Binary Data

The binary data type stores binary information such as images, files, or encrypted data. Binary data is often used when working with non-textual data.

**Example: The value stored in the binaryValue field is of binary type.

BinaryData

10. Date

Date data type stores date. It is a 64-bit integer which represents the number of milliseconds. BSON data type generally supports UTC datetime and it is signed. If the value of the date data type is negative then it represents the dates before 1970. There are various methods to return date, it can be returned either as a string or as a date object. Some method for the date handling:

**Example: We are using all the above method of the date:

Date

11. Min & Max key

The MinKey and MaxKey data types are used for internal comparisons. MinKey represents the lowest BSON element, while MaxKey represents the highest.

**Example:

minandmaxkey

12. Regular Expression

The regular expression data type is used to store regex patterns. MongoDB supports regex queries for performing pattern matching on string fields.

**Example: We are storing the regular expression gfg:

RegularExpression

13. JavaScript

MongoDB allows us to store JavaScript code within documents using the JavaScript data type. This can be useful for storing code or expressions.

**Example: We are using the JavaScript syntax in the shell:

JS

14. JavaScript with Scope

The JavaScript with Scope data type in MongoDB was used to store JavaScript code along with its scope (variables and functions). This feature has been deprecated in MongoDB 4.4 and is no longer recommended for use.

**Example: In this example, we are using the JavaScript syntax in the shell:

15. Timestamp

In MongoDB, this data type is used to store a timestamp. It is useful when we modify our data to keep a record and the value of this data type is 64-bit. The value of the timestamp data type is always unique.

**Example:

Timestamp

16. Decimal

This MongoDB data type stores 128-bit decimal-based floating-point value. This data type was introduced in MongoDB version 3.4

**Example:

Decimal

Decimal data type