Data Types in MongoDB

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.

  • Define how data is stored and processed in the database.
  • Impact query performance and indexing behavior.
  • Help maintain data consistency and efficient data modeling.

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:

  • age is stored as an integer.
  • It can be represented as either a 32-bit or 64-bit signed integer.
  • It is Suitable for storing whole numbers.

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:

  • marks is stored as a double to store the decimal value.
  • It is used to store floating-point numbers.
  • It is Ideal for data requiring decimal precision.

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.

  • the passed field is a boolean value indicating whether the student passed or failed.
  • It Stores true or false values.
  • It is Commonly used for binary states.

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.

  • Here, phone_number is set to null, indicating that the student has not provided a phone number.
  • It Represents the absence of data.
  • It is used for missing or undefined values.

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.

  • the skills field stores an array of strings representing the student's technical skills.
  • It is used to Store multiple values in a single field.
  • It can hold values of different data types.

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

  • Here, the book field stores an embedded document with properties like title, author, and published.
  • It is used to Store nested documents.
  • It is useful for hierarchical data structures.

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:

  • 4-bytes for Timestamp value.
  • 5-bytes for Random values. i.e., 3-bytes for machine Id and 2-bytes for process Id.
  • 3- bytes for Counter

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

  • MongoDB automatically generates a unique _id for each document.
  • Unique identifier for each document.
  • Automatically generated by MongoDB.

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.

  • the duration field is set to undefined.
  • It is used to represents an undefined value.
  • It is typically replaced by null value.

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

  • Here, the image field stores binary data.
  • It is used for storing non-textual data (e.g., images, files).
  • It is stored as binary data.

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:

  • Date(): It returns the current date in string format.
  • new Date(): It returns a date object. It uses the ISODate() wrapper. 
  • new ISODate(): It also returns a date object. It uses the ISODate() wrapper.

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

Date

  • the created_at field stores the current date and time.
  • It is used to store dates and times as 64-bit integers.
  • It is commonly used for timestamps.

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

  • It is used for internal comparisons in queries.
  • It is used to represent the lowest and highest BSON elements.

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

  • The name field stores a regular expression to match names that contain "John" case-insensitively.
  • It is used for pattern matching with regular expressions.
  • It is useful for querying string data.

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

  • It is used to store JavaScript code.
  • It is useful for embedded code or expressions.

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:

  • It is used for storing JavaScript code with scope.
  • it is deprecated in MongoDB 4.4.
  • It Uses alternatives like aggregation pipelines or external languages for dynamic code execution.

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

  • The updated_at field stores a timestamp value.
  • It is used for time tracking.
  • It is used to store a 64-bit value

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
Comment

Explore