MongoDB Applied Design Patterns

Book description

Whether you’re building a social media site or an internal-use enterprise application, this hands-on guide shows you the connection between MongoDB and the business problems it’s designed to solve. You’ll learn how to apply MongoDB design patterns to several challenging domains, such as ecommerce, content management, and online gaming. Using Python and JavaScript code examples, you’ll discover how MongoDB lets you scale your data model while simplifying the development process.

Many businesses launch NoSQL databases without understanding the techniques for using their features most effectively. This book demonstrates the benefits of document embedding, polymorphic schemas, and other MongoDB patterns for tackling specific big data use cases, including:

  • Operational intelligence: Perform real-time analytics of business data
  • Ecommerce: Use MongoDB as a product catalog master or inventory management system
  • Content management: Learn methods for storing content nodes, binary assets, and discussions
  • Online advertising networks: Apply techniques for frequency capping ad impressions, and keyword targeting and bidding
  • Social networking: Learn how to store a complex social graph, modeled after Google+
  • Online gaming: Provide concurrent access to character and world data for a multiplayer role-playing game

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Audience
    2. Assumptions This Book Makes
    3. Contents of This Book
      1. Part I: Design Patterns
      2. Part II: Use Cases
    4. Conventions Used in This Book
    5. Using Code Examples
    6. Safari® Books Online
    7. How to Contact Us
    8. Acknowledgments
  2. I. Design Patterns
    1. 1. To Embed or Reference
      1. Relational Data Modeling and Normalization
        1. What Is a Normal Form, Anyway?
        2. So What’s the Problem?
        3. Denormalizing for Performance
      2. MongoDB: Who Needs Normalization, Anyway?
        1. MongoDB Document Format
        2. Embedding for Locality
        3. Embedding for Atomicity and Isolation
        4. Referencing for Flexibility
        5. Referencing for Potentially High-Arity Relationships
        6. Many-to-Many Relationships
      3. Conclusion
    2. 2. Polymorphic Schemas
      1. Polymorphic Schemas to Support Object-Oriented Programming
      2. Polymorphic Schemas Enable Schema Evolution
        1. Storage (In-)Efficiency of BSON
          1. Object-Document Mappers
      3. Polymorphic Schemas Support Semi-Structured Domain Data
      4. Conclusion
    3. 3. Mimicking Transactional Behavior
      1. The Relational Approach to Consistency
      2. Compound Documents
      3. Using Complex Updates
      4. Optimistic Update with Compensation
      5. Conclusion
  3. II. Use Cases
    1. 4. Operational Intelligence
      1. Storing Log Data
        1. Solution Overview
        2. Schema Design
        3. Operations
          1. Inserting a log record
          2. Bulk inserts
          3. Finding all events for a particular page
          4. Aside: managing index size
          5. Finding all the events for a particular date
          6. Finding all events for a particular host/date
          7. Rules of index design
          8. Counting requests by day and page
        4. Sharding Concerns
          1. Limitations
          2. Option 1: Shard by time
          3. Option 2: Shard by a semi-random key
          4. Option 3: Shard by an evenly distributed key in the data set
          5. Option 4: Shard by combining a natural and synthetic key
          6. Test with your own data
        5. Managing Event Data Growth
          1. Capped collections
          2. TTL collections
          3. Multiple collections, single database
          4. Multiple databases
      2. Pre-Aggregated Reports
        1. Solution Overview
        2. Schema Design
          1. One document per page per day, flat documents
          2. One document per page per day, hierarchical documents
          3. Separate documents by granularity level
        3. Operations
          1. Log an event
          2. Pre-allocate
          3. Retrieving data for a real-time chart
          4. Get data for a historical chart
        4. Sharding Concerns
      3. Hierarchical Aggregation
        1. Solution Overview
        2. Schema Design
        3. MapReduce
        4. Operations
          1. Creating hourly views from event collections
          2. Deriving day-level data
          3. Weekly and monthly aggregation
          4. Refactor map functions
        5. Sharding Concerns
    2. 5. Ecommerce
      1. Product Catalog
        1. Solution Overview
          1. Concrete-table inheritance
          2. Single-table inheritance
          3. Multiple-table inheritance
          4. Entity attribute values
          5. Avoid modeling product data altogether
          6. The MongoDB answer
        2. Operations
          1. Find products sorted by percentage discount descending
          2. Find albums by genre and sort by year produced
          3. Find movies based on starring actor
          4. Find movies with a particular word in the title
          5. Conclusion: Index all the things!
        3. Sharding Concerns
          1. Scaling read performance without sharding
      2. Category Hierarchy
        1. Solution Overview
        2. Schema Design
        3. Operations
          1. Read and display a category
          2. Add a category to the hierarchy
          3. Change the ancestry of a category
          4. Rename a category
        4. Sharding Concerns
      3. Inventory Management
        1. Solution Overview
        2. Schema
        3. Operations
          1. Add an item to a shopping cart
          2. Modifying the quantity in the cart
          3. Checking out
          4. Returning inventory from timed-out carts
          5. Error handling
        4. Sharding Concerns
    3. 6. Content Management Systems
      1. Metadata and Asset Management
        1. Solution Overview
        2. Schema Design
          1. GridFS
        3. Operations
          1. Create and edit content nodes
          2. Upload a photo
          3. Locate and render a node
          4. Search for nodes by tag
          5. Generate a feed of recently published blog articles
        4. Sharding Concerns
      2. Storing Comments
        1. Solution Overview
        2. Approach: One Document per Comment
          1. Operation: Post a new comment
          2. Operation: View paginated comments
          3. Operation: Retrieve comments via direct links
        3. Approach: Embedding All Comments
          1. Operation: Post a new comment
          2. Operation: View paginated comments
          3. Operation: Retrieve a comment via direct links
        4. Approach: Hybrid Schema Design
          1. Operation: Post a new comment
          2. Operation: View paginated comments
          3. Operation: Retrieve a comment via direct links
        5. Sharding Concerns
    4. 7. Online Advertising Networks
      1. Solution Overview
      2. Design 1: Basic Ad Serving
        1. Schema Design
        2. Operation: Choose an Ad to Serve
        3. Operation: Make an Ad Campaign Inactive
        4. Sharding Concerns
      3. Design 2: Adding Frequency Capping
        1. Schema Design
        2. Operation: Choose an Ad to Serve
        3. Sharding
      4. Design 3: Keyword Targeting
        1. Schema Design
        2. Operation: Choose a Group of Ads to Serve
    5. 8. Social Networking
      1. Solution Overview
      2. Schema Design
        1. Independent Collections
        2. Dependent Collections
      3. Operations
        1. Viewing a News Feed or Wall Posts
        2. Commenting on a Post
        3. Creating a New Post
        4. Maintaining the Social Graph
      4. Sharding
    6. 9. Online Gaming
      1. Solution Overview
      2. Schema Design
        1. Character Schema
        2. Item Schema
        3. Location Schema
      3. Operations
        1. Load Character Data from MongoDB
        2. Extract Armor and Weapon Data for Display
        3. Extract Character Attributes, Inventory, and Room Information for Display
        4. Pick Up an Item from a Room
        5. Remove an Item from a Container
        6. Move the Character to a Different Room
        7. Buy an Item
      4. Sharding
    7. Afterword
      1. Where Do I Go from Here?
  4. Index
  5. About the Author
  6. Colophon
  7. Copyright

Product information

  • Title: MongoDB Applied Design Patterns
  • Author(s): Rick Copeland
  • Release date: March 2013
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449340049