MongoDB Data Types and Document Structure
Now that you have MongoDB installed and running, let's dive into the fundamental building blocks of MongoDB: data types and document structure. Understanding these concepts is crucial before we start performing CRUD operations in the next lessons.
Learning Goals:
- Understand MongoDB's document-oriented data model
- Learn all MongoDB data types with practical examples
- Master document structure and field naming conventions
- Recognize valid vs. invalid document patterns
Understanding MongoDB Documents
In MongoDB, data is stored as documents in BSON format (Binary JSON). Think of documents as the equivalent of rows in relational databases, but with much more flexibility.
{
"_id": ObjectId("507f1f77bcf86cd799439011"),
"name": "Alice Johnson",
"age": 28,
"email": "alice@example.com",
"isActive": true,
"hobbies": ["reading", "hiking", "photography"],
"address": {
"street": "123 Main St",
"city": "Springfield",
"zip": "12345"
},
"createdAt": ISODate("2023-10-15T14:30:00Z")
}
Documents are similar to JSON objects but with important enhancements: they support more data types, are stored in binary format for efficiency, and have a maximum size of 16MB.
MongoDB Data Types
Let's explore the most commonly used data types in MongoDB.
Basic Scalar Types
// String
"Hello MongoDB"
// Number (stores both integers and floating-point)
42
3.14159
// Boolean
true
false
// Null
null
ObjectId and Date Types
// ObjectId - MongoDB's primary key
ObjectId("507f1f77bcf86cd799439011")
// Date
ISODate("2023-10-15T14:30:00Z")
new Date("2023-10-15")
Array and Embedded Document Types
// Array
["mongodb", "express", "react", "node"]
// Embedded Document (sub-document)
{
"name": "Bob Smith",
"contact": {
"phone": "+1234567890",
"email": "bob@company.com"
},
"skills": ["JavaScript", "Python", "Database Design"]
}
Less Common but Important Types
// Binary Data
BinData(0, "SGVsbG8gV29ybGQ=")
// Regular Expression
/^mongodb/i
// 32-bit Integer
NumberInt(42)
// 64-bit Integer
NumberLong(9007199254740991)
// Timestamp
Timestamp(1697385600, 1)
Document Structure Best Practices
Field Names and Naming Conventions
{
"_id": ObjectId("507f1f77bcf86cd799439011"),
"firstName": "John",
"lastName": "Doe",
"emailAddress": "john@example.com",
"dateOfBirth": ISODate("1990-05-15"),
"isActiveUser": true
}
Avoid using special characters in field names. While MongoDB allows $ and . in some contexts, they have special meaning and can cause unexpected behavior.
Embedded Documents vs. References
// Good for one-to-few relationships
{
"user": "Alice",
"orders": [
{
"orderId": "ORD001",
"product": "Laptop",
"quantity": 1,
"price": 999.99
},
{
"orderId": "ORD002",
"product": "Mouse",
"quantity": 2,
"price": 25.50
}
]
}
// Good for many-to-many relationships
{
"_id": "USER001",
"name": "Alice",
"orderIds": ["ORD001", "ORD002"]
}
// In separate orders collection
{
"_id": "ORD001",
"userId": "USER001",
"product": "Laptop",
"price": 999.99
}
Working with Documents in Practice
Let's see how these concepts work together in a real-world example:
- JavaScript Example
- Python Example
const userDocument = {
_id: ObjectId("64a1b2c3d4e5f67890123456"),
username: "dev_guru",
profile: {
firstName: "Sarah",
lastName: "Chen",
birthDate: new Date("1992-08-20"),
avatar: BinData(0, "base64encodedimage")
},
preferences: {
theme: "dark",
notifications: true,
language: "en"
},
tags: ["developer", "mongodb", "javascript"],
stats: {
loginCount: NumberInt(142),
lastLogin: new Date(),
score: NumberLong(9999999999)
},
createdAt: new Date(),
updatedAt: new Date()
};
from bson import ObjectId, Binary
from datetime import datetime
user_document = {
"_id": ObjectId("64a1b2c3d4e5f67890123456"),
"username": "dev_guru",
"profile": {
"firstName": "Sarah",
"lastName": "Chen",
"birthDate": datetime(1992, 8, 20),
"avatar": Binary(b"binary_image_data")
},
"preferences": {
"theme": "dark",
"notifications": True,
"language": "en"
},
"tags": ["developer", "mongodb", "javascript"],
"stats": {
"loginCount": 142,
"lastLogin": datetime.now(),
"score": 9999999999
},
"createdAt": datetime.now(),
"updatedAt": datetime.now()
}
Common Pitfalls
- Field name restrictions: Avoid starting field names with
$or containing.as they have special meaning in MongoDB - Document size limit: Remember the 16MB maximum document size when designing your schema
- Type consistency: MongoDB is type-sensitive -
"42"(string) is different from42(number) - Date handling: Always use proper Date objects rather than strings for dates to enable date queries
- ObjectId generation: Let MongoDB generate
_idvalues unless you have specific needs
Summary
In this lesson, you learned:
- MongoDB stores data as BSON documents with rich data types
- Key data types include String, Number, Boolean, Array, Object, Date, and ObjectId
- Documents can contain embedded documents and arrays for complex data structures
- Field naming follows conventions and has some restrictions
- Understanding document structure is essential for effective MongoDB usage
Show quiz
-
What is the maximum size of a MongoDB document?
- A) 1MB
- B) 16MB
- C) 100MB
- D) No limit
-
Which data type is used for MongoDB's primary key
_idfield by default?- A) String
- B) Number
- C) ObjectId
- D) UUID
-
What's the main difference between embedded documents and references?
- A) Embedded documents are faster but have size limits
- B) References are always better for performance
- C) Embedded documents can't contain arrays
- D) References have a 1MB size limit
-
Why should you avoid using
$at the start of field names?- A) It's invalid syntax
- B) MongoDB reserves
$for operators - C) It causes documents to be rejected
- D) It makes queries slower
-
What happens if you store a number as a string like
"42"instead of42?- A) MongoDB automatically converts it
- B) Range queries and mathematical operations won't work as expected
- C) The document is rejected
- D) It uses more storage space automatically
Answers:
- B) 16MB
- C) ObjectId
- A) Embedded documents are faster but have size limits
- B) MongoDB reserves
$for operators - B) Range queries and mathematical operations won't work as expected