PacktLib: Ruby and MongoDB Web Development Beginner's Guide

Ruby and MongoDB Web Development Beginner's Guide

Credits

About the Author

Acknowledgement

About the Reviewers

www.PacktPub.com

Preface

Installing MongoDB and Ruby

Installing Ruby

Installing MongoDB

Configuring the MongoDB server

Starting MongoDB

Stopping MongoDB

The MongoDB CLI

Installing Rails/Sinatra

Summary

Diving Deep into MongoDB

Creating documents

Time for action — creating our first document

Using MongoDB embedded documents

Time for action — embedding reviews and votes

Using MongoDB document relationships

Time for action — creating document relations

Comparing MongoDB versus SQL syntax

Using Map/Reduce instead of join

Time for action — writing the map function for calculating vote statistics

Time for action — writing the reduce function to process emitted information

Understanding the Ruby perspective

Time for action — creating the project

Time for action — start your engines

Time for action — configuring Mongoid

Time for action — planning the object schema

Time for action — putting it all together

Time for action — adding reviews to books

Time for action — embedding Lease and Purchase models

Time for action — writing the map function to calculate ratings

Time for action — writing the reduce function to process the emitted results

Time for action — working with Map/Reduce using Ruby

Summary

MongoDB Internals

Understanding Binary JSON

What is ObjectId?

Documents and collections

JavaScript and MongoDB

Time for action — writing our own custom functions in MongoDB

Ensuring write consistency or "read your writes"

Global write lock

Transactional support in MongoDB

Time for action — implementing optimistic locking

Why are there no joins in MongoDB?

Summary

Working Out Your Way with Queries

Searching by fields in a document

Time for action — searching by a string value

Time for action — fetching only for specific fields

Time for action — skipping documents and limiting our search results

Time for action — finding books by name or publisher

Time for action — finding the highly ranked books

Searching inside arrays

Time for action — searching inside reviews

Searching inside hashes

Searching inside embedded documents

Searching with regular expressions

Time for action — using regular expression searches

Summary

Ruby DataMappers: Ruby and MongoDB Go Hand in Hand

Why do we need Ruby DataMappers

Time for action — using mongo gem

The Ruby DataMappers for MongoDB

Setting up DataMappers

Time for action — configuring MongoMapper

Time for action — setting up Mongoid

Creating, updating, and destroying documents

Time for action — creating and updating objects

Using finder methods

Using MongoDB criteria

Time for action — fetching using the where criterion

Understanding model relationships

Time for action — relating models

Time for action — categorizing books

Time for action — adding book details

Time for action — managing the driver entities

Time for action — creating vehicles using basic polymorphism

Using embedded objects

Time for action — creating embedded objects

Reverse embedded relations in Mongoid

Time for action — using embeds_one without specifying embedded_in

Time for action — using embeds_many without specifying embedded_in

Understanding embedded polymorphism

Time for action — adding licenses to drivers

Time for action — insuring drivers

Choosing whether to embed or to associate documents

Mongoid or MongoMapper — the verdict

Summary

Modeling Ruby with Mongoid

Developing a web application with Mongoid

Time for action — setting up a Rails project

Time for action — using Sinatra professionally

Defining attributes in models

Time for action — adding dynamic fields

Time for action — localizing fields

Using arrays and hashes in models

Defining relations in models

Time for action — configuring the many-to-many relation

Time for action — setting up the following and followers relationship

Time for action — setting up cyclic relations

Managing changes in models

Time for action — changing models

Mixing in Mongoid modules

Time for action — getting paranoid

Time for action — including a version

Summary

Achieving High Performance on Your Ruby Application with MongoDB

Profiling MongoDB

Time for action — enabling profiling for MongoDB

Using the explain function

Time for action — explaining a query

Using covered indexes

Time for action — using covered indexes

Other MongoDB performance tuning techniques

Understanding web application performance

Optimizing our code for performance

Optimizing and tuning the web application stack

Summary

Rack, Sinatra, Rails, and MongoDB — Making Use of them All

Revisiting Sodibee

The Rails way

Time for action — modeling the Author class

Time for action — writing the Book, Category and Address models

Time for action — modeling the Order class

Time for action — configuring routes

Time for action — writing the AuthorsController

Time for action — designing the layout

Time for action — listing authors

Time for action — adding new authors and books

The Sinatra way

Time for action — setting up Sinatra and Rack

Testing and automation using RSpec

Time for action — installing RSpec

Time for action — sporking it

Documenting code using YARD

Summary

Going Everywhere — Geospatial Indexing with MongoDB

What is geolocation

Identifying the exact geolocation

Storing coordinates in MongoDB

Time for action — geocoding the Address model

Time for action — saving geolocation coordinates

Time for action — using geocoder for storing coordinates

Firing geolocation queries

Time for action — finding nearby addresses

Time for action — firing near queries in Mongoid

Summary

Scaling MongoDB

High availability and failover via replication

Time for action — setting up the master/slave replication

Time for action — implementing replica sets

Implementing replica sets for Sodibee

Time for action — configuring replica sets for Sodibee

Implementing sharding

Time for action — setting up the shards

Time for action — starting the config server

Time for action — setting up mongos

Implementing Map/Reduce

Time for action — planning the Map/Reduce functionality

Time for action — Map/Reduce via the mongo console

Time for action — Map/Reduce via Ruby

Time for action — iterating Ruby objects

Summary

Pop quiz — Answers

Pop quiz — Answers

Pop quiz — Answers

Pop quiz — Answers

Pop quiz — Answers

Pop quiz — Answers

Pop quiz — Answers

Pop quiz — Answers