Book Image

Learning Mongoid

By : Gautam Rege
Book Image

Learning Mongoid

By: Gautam Rege

Overview of this book

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 (14 chapters)
Learning Mongoid
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Learning Mongoid gives beginners a good start to building a Rails application using MongoDB with Mongoid. For intermediate and expert Ruby developers, this book provides an excellent reference for using Mongoid. The book has plenty of examples with code samples and explanations that help in understanding the various features of Mongoid.

What this book covers

Chapter 1, What's so Awesome about Mongoid?, explains why Mongoid is one of the popular choices for Rails applications that use MongoDB. We are also introduced to Origin and Moped, which are integral parts of Mongoid.

Chapter 2, Mongoid Document Model, discusses the various data types that are supported by Mongoid and how they are stored. We learn about arrays, hashes, and embedded documents. We also briefly touch upon localized storage of string.

Chapter 3, Persisting Documents, discusses how documents are saved, format, optimization, and even managing custom data types and their serialization. We will also learn about atomic updates, callbacks, and validations.

Chapter 4, Mongoid Relations, explains the various relations that are supported by Mongoid, including One-to-One, One-to-Many, recursively embedded documents, and even polymorphic relations.

Chapter 5, Querying Attributes, explains everything about searching. We take a deep dive into how Origin works and how Mongoid criteria are managed. We learn about criteria chaining, lazy evaluation, and even eager loading of related documents.

Chapter 6, Performance Tuning, deals with how we can improve performance. We learn about the various indexes and when to use the right index. We see how the identity map can help in eager loading. Finally, we learn about MapReduce and the aggregation framework, which can drastically improve performance.

Chapter 7, Mongoid Modules, introduces various modules and gems available, which can help enhance the features with Mongoid. We learn about paranoid deletes, versioning, cloning, slugs, authentication, authorization using Devise, and even uploading documents using CarrierWave.

What you need for this book

This book assumes that you are using MacOS or Ubuntu Linux. It requires the latest MongoDB 2.4.x stable version, the latest Ruby 2.0 version, the latest Rails 4 version, and the latest Mongoid 4 version.

Who this book is for

This book is intended for beginners and experts alike. For a beginner, this book serves as an excellent guide for building Rails applications with Mongoid. For an intermediate or expert user, this book serves as an excellent reference for using the various features of Mongoid.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

class Address
  include Mongoid::Document

  field :street, type: String
  field :city, type: String
  field :state, type: String
  field :zipcode, type: String
  field :country, type: String

  embedded_in :author
end

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

require File.expand_path('../boot', __FILE__)

require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"

Any command-line input or output is written as follows:

$ ruby –v

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "clicking the Next button moves you to the next screen".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.