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.
{ "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.
{ "age": 25 }
Double (double
)
- Stores floating-point numbers.
{ "price": 249.48 }
Boolean (true/false
)
- Stores true/false values.
{ "isMarried": true }
Null
- Represents a null or missing value.
{ "middleName": null }
Arrays
- Stores multiple values in a single field.
{ "skills": ["PHP", "Python", "MongoDB"] }
Object / Embedded Document
- Stores a document inside another document.
{ "address": { "city": "Delhi", "pin": 110001 } }
ObjectId
- A unique 12-byte ID is automatically generated for each document in a collection.
{ "_id": ObjectId("507f1f77bcf86cd799439011") }
Date
- Stores the current date/time in UTC format.
{ "createdAt": new Date(), "dob" : ISODate("2002-05-01T12:30:00Z") }
Regular Expression (regex
)
- Stores regex patterns for advanced queries.
{ "name": { "$regex": "^A" } }
Binary Data
- Stores binary data (e.g., images, files).
{ "file": BinData(0,"YW55IGNhcm5hbCBwbGVhc3VyZQ==") }
Timestamp
- Records a timestamp, often used internally by MongoDB for replication.
{ "lastUpdatedDate": Timestamp() }
The post MongoDB Data Types appeared first on PHPGurukul.
Source: Read MoreÂ