In MongoDB, data types are flexible and documents within a collection can have different structures. However, MongoDB has some common data types that are frequently used. Here are some of them with examples:
1. String: Represents UTF-8 encoded strings.
{
"name": "John Doe",
"email": "john@example.com"
}
2. Integer: Represents 32-bit signed integers.
{
"age": 30,
"quantity": -10
}
3. Double: Represents 64-bit floating-point numbers.
{
"score": 95.5,
"price": 9.99
}
4. Boolean: Represents a boolean value (true or false).
{
"active": true,
"deleted": false
}
5. Date: Represents a date and time value.
{
"created_at": ISODate("2022-01-01T08:00:00Z"),
"updated_at": new Date()
}
6. Array: Represents an ordered collection of values.
{
"tags": ["mongodb", "database", "nosql"]
}
7. Object: Represents embedded documents.
{
"address": {
"street": "123 Main St",
"city": "New York",
"zip": "10001"
}
}
8. ObjectId: Represents a unique identifier for documents.
{
"_id": ObjectId("61f75ab4ff67c4418b3087c0"),
"name": "John Doe"
}
9. Binary: Represents binary data.
{
"avatar": BinData(0, "base64_encoded_data")
}
10. Null: Represents a null value.
{
"notes": null
}
These are some commonly used data types in MongoDB, and they can be used to define fields in documents within collections. MongoDB's flexible schema allows for dynamic document structures and nested data types.
No comments:
Post a Comment