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

Learning Mongoid

Book Description

If you know MongoDB and Ruby, then Mongoid is a very handy tool to have at your disposal. Quickly learn to build Rails applications with the helpful code samples and instructions in this book.

  • A step-by-step guide that explains how to use Mongoid through lots of examples and code
  • Monitor and fine-tune the performance of your application
  • Work with the MongoDB aggregation framework

In Detail

Mongoid helps you to leverage the power of schema-less and efficient document-based design, dynamic queries, and atomic modifier operations. Mongoid eases the work of Ruby developers while they are working on complex frameworks.

Starting with why and how you should use Mongoid, this book covers the various components of Mongoid. It then delves deeper into the detail of queries and relations, and you will learn some tips and tricks on improving performance. With this book, you will be able to build robust and large-scale web applications with Mongoid and Rails.

Starting with the basics, this book introduces you to components such as moped and origin, and how information is managed, learn about the various datatypes, embedded documents, arrays, and hashes. You will learn how a document is stored and manipulated with callbacks, validations, and even atomic updates. This book will then show you the querying mechanism in detail, right from simple to complex queries, and even explains eager loading, lazy evaluation, and chaining of queries. Finally, this book will explain the importance of performance tuning and how to use the right indexes. It also explains MapReduce and the Aggregation Framework.

Table of Contents

  1. Learning Mongoid
    1. Table of Contents
    2. Learning Mongoid
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers and more
        1. Why Subscribe?
        2. Free Access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. What's so Awesome about Mongoid?
      1. A practical approach using the Sodibee library system
        1. Checking prerequisites
          1. Ruby version
          2. MongoDB version
        2. Setting up Sodibee
          1. Creating models
          2. Testing the models
        3. Introducing Moped
          1. Dynamic attributes
        4. Introducing Origin
      2. Notice about Mongoid 2.x
      3. Mongoid and MongoMapper
      4. Summary
    9. 2. Mongoid Document Model
      1. The Mongoid document
        1. Basic attributes
        2. Arrays and hashes
        3. Date and time attributes
        4. Serialization with Mongoize
        5. Field aliases
      2. Embedded documents
      3. Localization
      4. Mass assignment and security
      5. Summary
    10. 3. Persisting Documents
      1. MongoDB persistence strategy
        1. Binary JSON (BSON)
        2. Memory maps, delayed sync, and journals
        3. Creating documents
        4. Updating documents and attributes
          1. Database locks and transactions
          2. Applications and lock versioning
          3. The MongoDB findAndModify method
        5. Atomic attribute updates
        6. Dynamic attributes
        7. Nested attributes
      2. Validations
      3. Callbacks
      4. Summary
    11. 4. Mongoid Relations
      1. Configuring relationships
        1. Common options for all relations
          1. :class_name
          2. :inverse_of
        2. Relation-specific options
      2. has_one – the one-to-one relation
        1. :as
        2. :autosave
        3. :autobuild
        4. :dependent
        5. :primary_key
        6. :foreign_key
        7. :validate
      3. has_many – the many-to-one relation
        1. :order
        2. :before_add, :after_add, :before_remove, and :after_remove
      4. The belongs_to relation
        1. :index
        2. :polymorphic
        3. :touch
        4. :counter_cache
      5. has_and_belongs_to_many – the many-to-many relation
        1. :inverse_of
      6. Polymorphic relations
      7. embeds_many – embedding many documents
        1. :cascade_callbacks
        2. :store_as
        3. :cyclic
      8. embeds_one – embed one document
      9. embedded_in – resolving children
      10. Embedded polymorphic relations
      11. Summary
    12. 5. Mongoid Queries
      1. Origins of Origin
      2. Query selectors
        1. Overloading the Symbol class
      3. Mongoid criteria
      4. Queries on arrays and hashes
        1. Scopes
      5. Atomic modifiers
        1. find_and_modify
      6. Querying with indexed fields
        1. Covered queries
      7. The aggregation framework
      8. Geolocation queries
      9. Summary
    13. 6. Performance Tuning
      1. MongoDB environment
      2. Indexes
        1. Write concern
        2. Compound index
        3. Multikey index
        4. Hashed index
        5. Text index
      3. IdentityMap
        1. n+1 query problem and eager loading
      4. MapReduce and the aggregation framework
        1. Using the aggregation framework
        2. Choosing between MapReduce and the aggregation framework
      5. Monitoring query performance
        1. Profiling MongoDB
        2. Statistical data
        3. Hint and currentOp
      6. Good practices and recommendations
      7. Summary
    14. 7. Mongoid Modules
      1. Timestamping
      2. Versioning
      3. Paranoia
      4. Acts as state machine (aasm)
        1. carrierwave
        2. mongoid-audit
        3. devise
      5. Summary
    15. Index