Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learning Mongoid
Learning Mongoid

Learning Mongoid: 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.

By Gautam Rege
$19.99 $13.98
Book Dec 2013 140 pages 1st Edition
eBook
$19.99 $13.98
Print
$32.99
Subscription
$15.99 Monthly
eBook
$19.99 $13.98
Print
$32.99
Subscription
$15.99 Monthly

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Dec 12, 2013
Length 140 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781782167501
Vendor :
MongoDB
Category :
Table of content icon View table of contents Preview book icon Preview Book

Learning Mongoid

Chapter 1. What's so Awesome about Mongoid?

The name MongoDB is derived from humongous. We use DataMappers to work with MongoDB. In Ruby, the most popular MongoDB mapper is Mongoid, pronounced mann-gyod. But is this the only mapper available? There also exists MongoMapper, MongoODM, and in the realm of open source there could well be many more!

So, what is so awesome about Mongoid? In a nutshell, its ability to gel with the Rails framework makes it very popular. For me, it is this adaptability to refactor and improve that makes Mongoid a very close ally of Rails.

In this chapter, we shall get a taste of the power of Mongoid. We shall see:

  • How Mongoid adheres to Rails ActiveModel and ActiveRelation syntax

  • Differences between Mongoid Version 2.x and 3.x

  • A brief introduction to Moped and Origin

  • Some differences between MongoMapper and Mongoid and their usage

A practical approach using the Sodibee library system


Sodibee (pronounced saw-di-bee) is a library-management system that can manage books, reviews, authors, and bookings. Here are some of the functions that are supported in Sodibee:

  • An author has many books and a book belongs to an author

  • A book has many reviews and has one booking

  • A review belongs to a user and is about a book

  • A booking belongs to a user and a book

Note

In the course of this book we will be working with the latest versions of Ruby 2.0, Mongoid 4, Rails 4, and MongoDB 2.4.

Checking prerequisites

First and foremost, we need to ensure that we have our development environment set up. It's common to use multiple versions of Ruby for development; I use RVM to manage these versions. As we can have multiple versions of the same gem installed on our machines, we use RVM gemsets to manage the gems we need for our work.

Ruby version

To check the Ruby version, check the version that is installed using the following command:

$ rvm list

rvm rubies

   jruby-1.7.4 [ x86_64 ]
   ruby-1.9.3-p385 [ x86_64 ]
=* ruby-2.0.0—p247 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

$ ruby –v
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.0]

Note

Ruby 2.1 is due to be released in December, 2013. Everything in this book will be fully compatible with Ruby 2.1 too.

MongoDB version

We are currently using MongoDB v2.4.6—verify that, using the following command:

$ mongo
MongoDB shell version: 2.4.6
connecting to: test
>

If you don't see this, it's quite likely that you have not installed MongoDB or it isn't running. Get going!

Setting up Sodibee

First and foremost, let's install the Rails gem.

$ gem install rails

This installs Rails 4.0.0.

Note

At the time of writing this book, the Rails version was 4.0.0. All commands would be fully compatible with the latest Rails version.

Now, let's create the Sodibee project.

$ rails new sodibee -O -T

This creates a new Rails project. The –O option tells Rails to skip ActiveRecord (we don't need it), and –T tells Rails to skip test unit. (We plan to use rspec later).

Tip

When you run the preceding command, it initiates a bundle install and updates our bundle with the default gems. If you are as impatient as I am, you may interrupt the process and press Ctrl + C to stop it, as we need to modify Gemfile to add other gems anyway.

Now, open Gemfile and configure for Mongoid.

gem 'mongoid', git: "git://github.com/mongoid/mongoid.git"

Note

Mongoid master is currently in sync with Rails 4. So, if we install using the released gem, it will install Version 3.x.

We're almost done. Issue the following command to update the bundle with our Mongoid gem:

$ bundle

If you did not use the –O option, you can run the following instructions to remove ActiveRecord from the application as we don't need it. Check and remove database.yml under config, if it has been generated. Next ensure that application.rb under config has the following lines:

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

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

# Assets should be precompiled for production (so we don't need the gems loaded then)
Bundler.require(*Rails.groups(assets: %w(development test)))

Tip

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.

Notice that there is no require "rails/all". This ensures that the ActiveRecord railtie is not loaded. However, sometimes this causes a conflict with the environment settings. So, in case you face a problem starting the Rails console, remove the following line from development.rb under config/environments (and as required from the other environment files):

# config.active_record.migration_error = :page_load

This should get us going. Now issue the following command to set up Mongoid:

$ bundle exec rails generate mongoid:config

This generates mongoid.yml under config.

Test this basic Rails setup by starting the console.

$ rails c
Loading development environment (Rails 4.0.0)
2.0.0-p247 :001 >

If you see the preceding command prompt, we are set.

Creating models

Now that we have our environment set up, let's create our basic models. In the Author model, we shall now add a field called name, and create a relation between the Author model and the Address model.

$ rails generate model Author
# app/models/author.rb
class Author
  include Mongoid::Document
  include Mongoid::Attributes::Dynamic

  field :name, type: String

  embeds_one :address
end

Now, let's create the Address model with a number of fields and relations.

$ rails generate model Address
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

Now, let's test the code that we have written.

Note

Did you notice that we are not using the hash rocket (=>) notation for defining the hash of options for the field method? Instead of :type => String, we are using the JSON notation instead. We shall follow this standard throughout the book.

Testing the models

Now that we have created the models, let's test it out quickly:

irb> a = Author.create(name: "Charles Dickens")
 => #<Author _id: 5143678345db7ca255000001, name: "Charles Dickens">

irb> a.create_address(street: "Picadilly Circus", city: "London", country: "UK")
 => #<Address _id: 514367f445db7ca255000003, street: "Picadilly Circus", city: "London", state: nil, zipcode: nil, country: "UK">

As we can see, this creates an Author object and its corresponding Address object. Mongoid includes ActiveModel and you may notice the similarity in these methods if you have used ActiveRecord.

Tip

We have used create_address because an author has only one embedded address. If, an author had multiple addresses, we would have used a.addresses.create.

irb> Author.first
 => #<Author _id: 5143678345db7ca255000001, name: "Charles Dickens">

irb> Author.first.address
 => #<Address _id: 514367f445db7ca255000003, street: "Picadilly Circus", city: "London", state: nil, zipcode: nil, country: "UK">

Here, we have double-checked that the author is indeed persisted to the database. Since this is MongoDB, we can dynamically add attributes to the object!

irb> a['language'] = "English"
 => "English"

irb> a.save
 => true

irb> Author.first
 => #<Author _id: 5143678345db7ca255000001, name: "Charles Dickens", language: "English">

Introducing Moped

So, let's see what happened in Mongoid and MongoDB. First, let's see what is in the log file development.log under log.

When we issued the command Author.create(name: "Charles Dickens"), it generated the following output:

MOPED: 127.0.0.1:27017 INSERT       database=sodibee_development collection=authors documents=[{"_id"=>"5143678345db7ca255000001", "name"=>"Charles Dickens"}] flags=[] (0.2460ms)

Now, when we issued the second command a.create_address(street: "Picadilly Circus", city: "London", country: "UK"), it updated the Author object, and created an embedded Address document as seen in the following line:

MOPED: 127.0.0.1:27017 UPDATE       database=sodibee_development collection=authors selector={"_id"=>"5143678345db7ca255000001"} update={"$set"=>{"address"=>{"_id"=>"514367f445db7ca255000003", "street"=>"Picadilly Circus", "city"=>"London", "country"=>"UK"}}} flags=[] (0.1211ms)

Now that we have seen what INSERT and UPDATE look like, querying the Author collection with Author.first generates the following result:

MOPED: 127.0.0.1:27017 QUERY        database=sodibee_development collection=authors selector={"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (66.7090ms)

And since we want to query the address, we look it up using Author.first.address. This generates the following line:

MOPED: 127.0.0.1:27017 QUERY        database=sodibee_development collection=authors selector={"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.5021ms)

Now there's something interesting about the preceding output—the last two commands on the Author model fired the same query, and look at the difference in the query result! The same query is fired because the address is an embedded document. So, to fetch the address of an author, you fetch the Author object itself. The difference of 66 ms and 0.5 ms in the query response is because for the first lookup MongoDB loads the document from the disk and puts it into its memory-mapped file. The second time, the document is simply looked up in cache (the memory-mapped file) and hence the lookup is faster.

Dynamic attributes

When we issued the command a['language'] = "English", and saved the object using a.save; this is what we see:

  MOPED: 127.0.0.1:27017 UPDATE       database=sodibee_development collection=authors selector={"_id"=>"5143678345db7ca255000001"} update={"$set"=>{"language"=>"English"}} flags=[] (0.1121ms)

This is the result of dynamic attribute update. Even though we did not specify language as a field in the Author model, we can set it as an attribute for the Author object. Did you notice that the update for dynamic attributes is no different from the standard update query in MongoDB?

However, there is a difference when accessing it in Mongoid. The Author.first.language parameter may throw an error sometimes, but Author.first[:language] will always succeed. Let's see an example:

irb> a = Author.create(name: "Gautam")
 => #<Author _id: 515085fd45db7c911e000003, name: "Gautam">

Here we have created a new Author object. However, when we try to update the object using the dot notation a.language, it gives an error. As we can see in the following command lines, method_missing does not dynamically create the accessor method if the dynamic attribute does not already exist.

irb> a.language = "English"
NoMethodError: undefined method `language=' for #<Author _id: 515085fd45db7c911e000003, name: "Gautam">
  from lib/mongoid/attributes.rb:317:in `method_missing'
  from (irb):12
  from lib/rails/commands/console.rb:88:in `start'
  from lib/rails/commands/console.rb:9:in `start'
  from lib/rails/commands.rb:64:in `<top (required)>'
  from bin/rails:4:in `require'
  from bin/rails:4:in `<main>'

Now, if we try to update the dynamic attribute without using the dot notation, it works!

irb> a[:language] = "English"
 => "English"
irb> a.save
 => true

Since we have saved it now, when we access the dynamic attribute language again, method_missing creates the accessor method because the dynamic attribute exists. So, now even the dot notation works.

irb> a.language
 => "English"
irb> a[:language]
 => "English"

Introducing Origin

Origin is a gem that provides the DSL for Mongoid queries. Though at first glance, a question may seem to arise as to why we need a DSL for Mongoid queries; If we are finally going to convert the query to a MongoDB-compliant hash, then why do we need a DSL?

Origin was extracted from Mongoid gem and put into a new gem, so that there is a standard for querying. It has no dependency on any other gem and is a standalone pure query builder. The idea was that this could be a generic DSL that can be used even without Mongoid!

So, now we have a very generic and standard querying pattern. For example, in Mongoid 2.x we had the criteria any_in and any_of, and no direct support for the and, or, and nor operations. In Mongoid 2.x, the only way we could fire a $or or a $and query was like this:

Author.where("$or" => {'language' => 'English', 'address.city' => 'London
'})

And now in Mongoid 3, we have a cleaner approach.

Author.or(:language => 'English', 'address.city' => 'London')

Origin also provides good selectors directly in our models. So, this is now much more readable:

Book.gte(published_at: Date.parse('2012/11/11'))

As we shall see later in the book, Origin has a lot more cool features.

Notice about Mongoid 2.x


It's important to realize that quite a bit has changed between the two major releases of Mongoid. It's an uncanny coincidence that just like Rails, major versions (2.x and 3.x) differ from each other vastly. Let's take a look at some of the differences between Mongoid 2.x and Mongoid 3.x.

  • Mongoid 3.x and later versions support Ruby 1.9.3 and onwards only.

  • There are entirely new options in mongoid.yml for database configuration.

  • Mongoid has changed its serialization mechanism.

  • Apart from these, there are quite a few more changes, which have made using Mongoid better and faster.

Mongoid and MongoMapper


It's also important to know about the other Ruby ODMs (Object Document Managers) besides Mongoid. Among the most popular ones out there, is MongoMapper.

  • MongoMapper, though older than Mongoid, has a slower evolution cycle. Unlike Mongoid, MongoMapper differentiates between documents and embedded documents.

  • MongoMapper uses a non-ActiveModel syntax such as many and one, where Mongoid uses has_many and has_one.

  • Mongoid has currently become more popular because of its adherence to the Rails ActiveModel and ActiveRelation syntax.

  • Mongoid has much better documentation than MongoMapper.

Summary


Mongoid has power. Just like Rails, it evolves fast. It has Moped and Origin that ensure speed, flexibility, and consistency.

In the upcoming chapters, we shall see what documents are, in the context of Mongoid. We shall learn about the different data types, dynamic attributes, and how data is managed.

The journey has just begun.

Left arrow icon Right arrow icon

Key benefits

  • 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

Description

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.

What you will learn

Learn the syntax of Mongoid 4 with Rails 4 Understand moped, the new Ruby driver for MongoDB Learn about document relations in Mongoid Understand origin, the new querying DSL Manage indexes in Mongoid Learn about gems that work with Mongoid Tune and monitor performance

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Dec 12, 2013
Length 140 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781782167501
Vendor :
MongoDB
Category :

Table of Contents

14 Chapters
Learning Mongoid Chevron down icon Chevron up icon
Credits Chevron down icon Chevron up icon
About the Author Chevron down icon Chevron up icon
About the Reviewers Chevron down icon Chevron up icon
www.PacktPub.com Chevron down icon Chevron up icon
Preface Chevron down icon Chevron up icon
What's so Awesome about Mongoid? Chevron down icon Chevron up icon
Mongoid Document Model Chevron down icon Chevron up icon
Persisting Documents Chevron down icon Chevron up icon
Mongoid Relations Chevron down icon Chevron up icon
Mongoid Queries Chevron down icon Chevron up icon
Performance Tuning Chevron down icon Chevron up icon
Mongoid Modules Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.