MongoDB Data Types
Below are the main data types in MongoDB:
- String (“text”)
- Integer (int32, int64)
- Double (double)
- Boolean (true/false)
- Null
- Arrays
- Object / Embedded Document
- ObjectId
- Date
- Regular Expression (regex)
- Binary Data
- Timestamp
String (“text”)
- Most common data type.
- Stores text values in UTF-8 format.
1 |
{ "name": "Anuj Kumar" } |
Integer (int32
, int64
)
- Stores numeric values.
- int32: For smaller numbers (−2,147,483,648 to 2,147,483,647).
- int64: For larger numbers.
1 |
{ "age": 25 } |
Double (double
)
- Stores floating-point numbers.
1 |
{ "price": 249.48 } |
Boolean (true/false
)
- Stores true/false values.
1 |
{ "isMarried": true } |
Null
- Represents a null or missing value.
1 |
{ "middleName": null } |
Arrays
- Stores multiple values in a single field.
1 |
{ "skills": ["PHP", "Python", "MongoDB"] } |
Object / Embedded Document
- Stores a document inside another document.
1 2 3 |
{ "address": { "city": "Delhi", "pin": 110001 } } |
ObjectId
- A unique 12-byte ID is automatically generated for each document in a collection.
1 |
{ "_id": ObjectId("507f1f77bcf86cd799439011") } |
Date
- Stores the current date/time in UTC format.
1 2 3 4 |
{ "createdAt": new Date(), "dob" : ISODate("2002-05-01T12:30:00Z") } |
Regular Expression (regex
)
- Stores regex patterns for advanced queries.
1 |
{ "name": { "$regex": "^A" } } |
Binary Data
- Stores binary data (e.g., images, files).
1 |
{ "file": BinData(0,"YW55IGNhcm5hbCBwbGVhc3VyZQ==") } |
Timestamp
- Records a timestamp, often used internally by MongoDB for replication.
1 |
{ "lastUpdatedDate": Timestamp() } |