Skip to main content

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.

Example User Document
{
"_id": ObjectId("507f1f77bcf86cd799439011"),
"name": "Alice Johnson",
"age": 28,
"email": "[email protected]",
"isActive": true,
"hobbies": ["reading", "hiking", "photography"],
"address": {
"street": "123 Main St",
"city": "Springfield",
"zip": "12345"
},
"createdAt": ISODate("2023-10-15T14:30:00Z")
}
tip

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​

Basic Data Types Example
// String
"Hello MongoDB"

// Number (stores both integers and floating-point)
42
3.14159

// Boolean
true
false

// Null
null

ObjectId and Date Types​

Special Types Example
// ObjectId - MongoDB's primary key
ObjectId("507f1f77bcf86cd799439011")

// Date
ISODate("2023-10-15T14:30:00Z")
new Date("2023-10-15")

Array and Embedded Document Types​

Complex Types Example
// Array
["mongodb", "express", "react", "node"]

// Embedded Document (sub-document)
{
"name": "Bob Smith",
"contact": {
"phone": "+1234567890",
"email": "[email protected]"
},
"skills": ["JavaScript", "Python", "Database Design"]
}

Less Common but Important Types​

Additional Data 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​

Good Field Naming
{
"_id": ObjectId("507f1f77bcf86cd799439011"),
"firstName": "John",
"lastName": "Doe",
"emailAddress": "[email protected]",
"dateOfBirth": ISODate("1990-05-15"),
"isActiveUser": true
}
warning

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​

Embedded Approach
// 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
}
]
}
Referenced Approach
// 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:

Complete User Document
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()
};

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 from 42 (number)
  • Date handling: Always use proper Date objects rather than strings for dates to enable date queries
  • ObjectId generation: Let MongoDB generate _id values 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
  1. What is the maximum size of a MongoDB document?

    • A) 1MB
    • B) 16MB
    • C) 100MB
    • D) No limit
  2. Which data type is used for MongoDB's primary key _id field by default?

    • A) String
    • B) Number
    • C) ObjectId
    • D) UUID
  3. 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
  4. 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
  5. What happens if you store a number as a string like "42" instead of 42?

    • 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:

  1. B) 16MB
  2. C) ObjectId
  3. A) Embedded documents are faster but have size limits
  4. B) MongoDB reserves $ for operators
  5. B) Range queries and mathematical operations won't work as expected