O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

MongoDB: The Definitive Guide, 3rd Edition

Book Description

With Early Release ebooks, you get books in their earliest form—the author's raw and unedited content as he or she writes—so you can take advantage of these technologies long before the official release of these titles. You’ll also receive updates when significant changes are made, new chapters are available, and the final ebook bundle is released.

Manage your data in a database system designed to support modern application development. The updated edition of this authoritative and accessible guide shows you the many advantages of using document-oriented databases, including how this secure, high-performance system enables flexible data models, high availability, and horizontal scalability.

Written by current and former members of the MongoDB team, the third edition is updated for MongoDB 4.0. You’ll find substantial updates on querying, indexing, aggregation, replica sets, ops manager, sharding administration, data administration, durability, monitoring, and security.

Authors Shannon Bradshaw (MongoDB) and Kristina Chodorow (Google) provide guidance for database developers, advanced configuration for system administrators, and use cases for a variety of projects. Ideal for NoSQL newcomers and experienced MongoDB users alike, this book also includes many real-world schema design examples.

Table of Contents

  1. I. Introduction to MongoDB
  2. 1. Introduction
    1. Ease of Use
    2. Designed to Scale
    3. Rich with Features...
    4. …Without Sacrificing Speed
    5. Let’s Get Started
  3. 2. Getting Started
    1. Documents
    2. Collections
      1. Dynamic Schemas
      2. Naming
    3. Databases
    4. Getting and Starting MongoDB
    5. Introduction to the MongoDB Shell
      1. Running the Shell
      2. A MongoDB Client
      3. Basic Operations with the Shell
    6. Data Types
      1. Basic Data Types
      2. Dates
      3. Arrays
      4. Embedded Documents
      5. _id and ObjectIds
    7. Using the MongoDB Shell
      1. Tips for Using the Shell
      2. Running Scripts with the Shell
      3. Creating a .mongorc.js
      4. Customizing Your Prompt
      5. Editing Complex Variables
      6. Inconvenient Collection Names
  4. 3. Creating, Updating, and Deleting Documents
    1. Inserting Documents
      1. insertMany()
      2. Insert Validation
      3. insert()
    2. Removing Documents
      1. drop()
    3. Updating Documents
      1. Document Replacement
      2. Using Update Operators
      3. Upserts
      4. Updating Multiple Documents
      5. Returning Updated Documents
  5. 4. Querying
    1. Introduction to find
      1. Specifying Which Keys to Return
      2. Limitations
    2. Query Criteria
      1. Query Conditionals
      2. OR Queries
      3. $not
      4. Conditional Semantics
    3. Type-Specific Queries
      1. null
      2. Regular Expressions
      3. Querying Arrays
      4. Querying on Embedded Documents
    4. $where Queries
      1. Server-Side Scripting
    5. Cursors
      1. Limits, Skips, and Sorts
      2. Avoiding Large Skips
      3. Advanced Query Options
      4. Immortal Cursors
    6. Database Commands
      1. How Commands Work
  6. II. Designing Your Application
  7. 5. Indexes
    1. Introduction to Indexes
      1. Creating an Index
      2. Introduction to Compound Indexes
      3. How MongoDB Selects an Index
      4. Using Compound Indexes
      5. How $-Operators Use Indexes
      6. Indexing Objects and Arrays
      7. Index Cardinality
    2. Using explain()
      1. The Query Optimizer
    3. When Not to Index
    4. Types of Indexes
      1. Unique Indexes
      2. Sparse Indexes
    5. Index Administration
      1. Identifying Indexes
      2. Changing Indexes
  8. 6. Special Index and Collection Types
    1. Geospatial Indexes
      1. Types of Geospatial Queries
      2. Using Geospatial Indexes
      3. Compound Geospatial Indexes
      4. 2D Indexes
    2. Indexes for Full Text Search
      1. Create a Text Index
      2. Text Search
      3. Optimize Full Text Search
      4. Searching in Other Languages
    3. Capped Collections
      1. Creating Capped Collections
      2. Tailable Cursors
    4. Time-To-Live (TTL) Indexes
    5. Storing Files with GridFS
      1. Getting Started with GridFS: mongofiles
      2. Working with GridFS from the MongoDB Drivers
      3. Under the Hood
  9. 7. Introduction to the Aggregation Framework
    1. Pipelines, Stages, and Tunables
    2. Getting Started with Stages: Familiar Operations
    3. Expressions
    4. Deeper Dive
    5. $unwind
    6. Array Expressions
    7. Accumulators
    8. Using Accumulators in Project Stages
    9. Introduction to Grouping
    10. The _id Field in Group Stages
    11. Group vs Project
  10. III. Replication
  11. 8. Setting Up a Replica Set
    1. Introduction to Replication
    2. Setting up a Replica Set, Part 1
    3. Networking Considerations
    4. Security Considerations
    5. Setting up a Replica Set, Part 2
    6. rs Helper Functions
    7. Observing Replication
    8. Changing Your Replica Set Configuration
    9. How to Design a Set
      1. How Elections Work
    10. Member Configuration Options
      1. Priority
      2. Hidden
      3. Creating Election Arbiters
      4. Building Indexes
  12. 9. Components of a Replica Set
    1. Syncing
      1. Initial Sync
      2. Replication
      3. Handling Staleness
    2. Heartbeats
      1. Member States
    3. Elections
    4. Rollbacks
      1. When Rollbacks Fail
  13. 10. Connecting to a Replica Set from Your Application
    1. Client-to-Replica-Set Connection Behavior
    2. Waiting for Replication on Writes
      1. Other Options for “w”
    3. Custom Replication Guarantees
      1. Guaranteeing One Server per Data Center
      2. Guaranteeing a Majority of Nonhidden Members
      3. Creating Other Guarantees
    4. Sending Reads to Secondaries
      1. Consistency Considerations
      2. Load Considerations
      3. Reasons to Read from Secondaries
  14. 11. Administration
    1. Starting Members in Standalone Mode
    2. Replica Set Configuration
      1. Creating a Replica Set
      2. Changing Set Members
      3. Creating Larger Sets
      4. Forcing Reconfiguration
    3. Manipulating Member State
      1. Turning Primaries into Secondaries
      2. Preventing Elections
    4. Monitoring Replication
      1. Getting the Status
      2. Visualizing the Replication Graph
      3. Replication Loops
      4. Disabling Chaining
      5. Calculating Lag
      6. Resizing the Oplog
      7. Building Indexes
      8. Replication on a Budget
  15. IV. Sharding
  16. 12. Introduction to Sharding
    1. Introduction to Sharding
    2. Understanding the Components of a Cluster
    3. A One-Minute Test Setup
  17. 13. Configuring Sharding
    1. When to Shard
    2. Starting the Servers
      1. Config Servers
      2. The mongos Processes
      3. Adding a Shard from a Replica Set
      4. Adding Capacity
      5. Sharding Data
    3. How MongoDB Tracks Cluster Data
      1. Chunk Ranges
      2. Splitting Chunks
    4. The Balancer
  18. 14. Choosing a Shard Key
    1. Taking Stock of Your Usage
    2. Picturing Distributions
      1. Ascending Shard Keys
      2. Randomly Distributed Shard Keys
      3. Location-Based Shard Keys
    3. Shard Key Strategies
      1. Hashed Shard Key
      2. Hashed Shard Keys for GridFS
      3. The Firehose Strategy
      4. Multi-Hotspot
    4. Shard Key Rules and Guidelines
      1. Shard Key Limitations
      2. Shard Key Cardinality
    5. Controlling Data Distribution
      1. Using a Cluster for Multiple Databases and Collections
      2. Manual Sharding
  19. 15. Sharding Administration
    1. Seeing the Current State
      1. Getting a Summary with sh.status
      2. Seeing Configuration Information
    2. Tracking Network Connections
      1. Getting Connection Statistics
      2. Limiting the Number of Connections
    3. Server Administration
      1. Adding Servers
      2. Changing Servers in a Shard
      3. Removing a Shard
    4. Balancing Data
      1. The Balancer
      2. Changing Chunk Size
      3. Moving Chunks
      4. Jumbo Chunks
      5. Refreshing Configurations
  20. V. Application Administration
  21. 16. An Introduction to MongoDB Security
    1. MongoDB Authentication and Authorization
      1. Authentication Mechanisms
      2. Authorization
      3. Using x.509 Certificates to Authenticate Both Members and Clients
    2. A Tutorial on MongoDB Authentication and Transport Layer Encryption
      1. Establish a Root CA
      2. Generate and Sign Member Certificates
      3. Generate and Sign Client Certificates
      4. Bring Up the Replica Set Without Authentication and Authorization Enabled
      5. Create the Admin User
      6. Restart the Replica Set with Authentication and Authorization Enabled
      7. Tutorial
  22. 17. Durability
    1. Durability at the Member Level Through Journaling
    2. Durability at the Cluster Level using Write Concern
      1. The “w” and “wtimeout” Options for writeConcern
      2. The “j” (journaling) Option for writeConcern
    3. Durability of Transactions using Write Concern
    4. What MongoDB Does Not Guarantee
    5. Checking for Corruption
  23. VI. Server Administration
  24. 18. Starting and Stopping MongoDB
    1. Starting from the Command Line
      1. File-Based Configuration
    2. Stopping MongoDB
    3. Security
      1. Data Encryption
      2. SSL Connections
    4. Logging
  25. 19. Monitoring MongoDB
    1. Monitoring Memory Usage
      1. Introduction to Computer Memory
      2. Tracking Memory Usage
      3. Tracking Page Faults
      4. IO Wait
    2. Calculating the Working Set
      1. Some Working Set Examples
    3. Tracking Performance
      1. Tracking Free Space
    4. Monitoring Replication
  26. 20. Making Backups
    1. Backup Methods
    2. Backing Up a Server
      1. Filesystem Snapshot
      2. Copying Data Files
      3. Using mongodump
    3. Backing Up a Replica Set
    4. Backing Up a Sharded Cluster
      1. Backing Up and Restoring an Entire Cluster
      2. Backing Up and Restoring a Single Shard
  27. 21. Deploying MongoDB
    1. Designing the System
      1. Choosing a Storage Medium
      2. Recommended RAID Configurations
      3. CPU
      4. Choosing an Operating System
      5. Swap Space
      6. Filesystem
    2. Virtualization
      1. Turn Off Memory Overcommitting
      2. Mystery Memory
      3. Handling Network Disk IO Issues
      4. Using Non-Networked Disks
    3. Configuring System Settings
      1. Turning Off NUMA
      2. Setting Readahead
      3. Disabling Hugepages
      4. Choosing a Disk Scheduling Algorithm
      5. Don’t Track Access Time
      6. Modifying Limits
    4. Configuring Your Network
    5. System Housekeeping
      1. Synchronizing Clocks
      2. The OOM Killer
      3. Turn Off Periodic Tasks