Data Types in MongoDB
Data Types in MongoDB
MongoDB supports a wide variety of data types that are used to define fields within documents. These types are essential for representing various forms of data and allow MongoDB to handle complex, structured, and unstructured data.
Here’s a breakdown of the most common data types in MongoDB:
1. String (string
)
- Description: Used to store textual data, similar to a typical string in other programming languages. Strings in MongoDB are UTF-8 encoded.
- Example:
{ "name": "Alice" }
2. Integer
MongoDB supports both 32-bit and 64-bit integers:
- 32-bit Integer (
int
): This stores numbers in the range of -2,147,483,648 to 2,147,483,647.{ "age": 25 }
- 64-bit Integer (
long
orNumberLong
): Used to store larger numbers beyond the range of 32-bit integers.{ "large_number": NumberLong("9223372036854775807") }
3. Double (double
)
- Description: Used to store floating-point numbers. It follows the IEEE 754 standard.
- Example:
{ "price": 199.99 }
4. Boolean (bool
)
- Description: Represents binary true or false values.
- Example:
{ "is_active": true }
5. Array (array
)
- Description: Stores an ordered list of values, which can contain multiple data types, including embedded documents or other arrays.
- Example:
{ "skills": ["JavaScript", "Python", "MongoDB"] }
6. Object/Document (object
)
- Description: A document or embedded object that stores key-value pairs, which can themselves be complex or nested structures.
- Example:
{ "address": { "street": "123 Main St", "city": "New York", "zip": 10001 } }
7. ObjectId (objectId
)
- Description: A 12-byte identifier that is unique for each document. MongoDB automatically generates an
_id
field for each document usingObjectId
unless another primary key is specified. - Structure: The 12 bytes are divided as:
- 4 bytes for a Unix timestamp (seconds since the epoch)
- 5 bytes for a random value (unique to the machine and process)
- 3 bytes for an incrementing counter
- Example:
{ "_id": ObjectId("507f1f77bcf86cd799439011") }
8. Date (date
)
- Description: Used to store date and time values. Internally, dates are stored as a 64-bit integer representing milliseconds since the Unix epoch (January 1, 1970). MongoDB's date type is much more flexible than the JSON date format.
- Example:
{ "created_at": ISODate("2024-09-01T12:00:00Z") }
9. Null (null
)
- Description: Represents a null or non-existent value.
- Example:
{ "middle_name": null }
10. Binary Data (binData
)
- Description: Used to store binary data, such as images, video files, or other non-textual data.
- Example:
{ "profile_pic": <binary> }
11. Regular Expression (regex
)
- Description: Used to store regular expressions for pattern matching.
- Example:
{ "pattern": /MongoDB/i }
12. Timestamp (timestamp
)
- Description: Used for internal MongoDB operations like replication. It's different from
date
and represents a timestamp in the form of a 64-bit value. - Example:
{ "event_time": Timestamp(1631544830, 1) }
13. Decimal128 (decimal
)
- Description: Used to store high-precision decimal values, especially useful for financial calculations where precision is critical.
- Example:
{ "price": NumberDecimal("1234.56") }
14. Min/Max Key (minKey
, maxKey
)
- Description:
- MinKey: Compares as less than any other BSON value.
- MaxKey: Compares as greater than any other BSON value.
- Usage: Mostly used in database operations like range queries.
- Example:
{ "lowest_value": MinKey() } { "highest_value": MaxKey() }
15. JavaScript Code (javascript
)
- Description: Allows you to store JavaScript code in a document. You can also store the code with a scope, which provides context for variable evaluation.
- Example:
{ "fn": function() { return "Hello, MongoDB!"; } }
Example Document Using Multiple Data Types
Here’s an example MongoDB document that uses a variety of data types:
{
"_id": ObjectId("507f191e810c19729de860ea"),
"name": "John Doe",
"age": 29,
"email": "johndoe@example.com",
"created_at": ISODate("2024-09-01T08:00:00Z"),
"is_active": true,
"score": NumberDecimal("99.9"),
"languages": ["English", "Spanish", "French"],
"address": {
"street": "123 Main St",
"city": "New York",
"zip": 10001
},
"profile_pic": <binary data>,
"regex_pattern": /john/i
}