Ruby and MongoDB Web Development Beginner's Guide


Ruby and MongoDB Web Development Beginner's Guide
eBook: $26.99
Formats: PDF, PacktLib, ePub and Mobi formats
$22.95
save 15%!
Print + free eBook + free PacktLib access to the book: $71.98    Print cover: $44.99
$67.94
save 6%!
Free Shipping!
UK, US, Europe and selected countries in Asia.
Also available on:
Overview
Table of Contents
Author
Support
Sample Chapters
  • Step-by-step instructions and practical examples to creating web applications with Ruby and MongoDB
  • Learn to design the object model in a NoSQL way
  • Create objects in Ruby and map them to MongoDB

Book Details

Language : English
Paperback : 332 pages [ 235mm x 191mm ]
Release Date : July 2012
ISBN : 1849515026
ISBN 13 : 9781849515023
Author(s) : Gautam Rege
Topics and Technologies : All Books, Web Development, Beginner's Guides, Open Source, Web Development

Table of Contents

Preface
Chapter 1: Installing MongoDB and Ruby
Chapter 2: Diving Deep into MongoDB
Chapter 3: MongoDB Internals
Chapter 4: Working Out Your Way with Queries
Chapter 5: Ruby DataMappers: Ruby and MongoDB Go Hand in Hand
Chapter 6: Modeling Ruby with Mongoid
Chapter 7: Achieving High Performance on Your Ruby Application with MongoDB
Chapter 8: Rack, Sinatra, Rails, and MongoDB – Making Use of them All
Chapter 9: Going Everywhere – Geospatial Indexing with MongoDB
Chapter 10: Scaling MongoDB
Pop Quiz Answers
Index
  • Chapter 1: Installing MongoDB and Ruby
    • Installing Ruby
      • Using RVM on Linux or Mac OS
        • The RVM games
        • The Windows saga
      • Using rbenv for installing Ruby
    • Installing MongoDB
    • Configuring the MongoDB server
    • Starting MongoDB
    • Stopping MongoDB
    • The MongoDB CLI
      • Understanding JavaScript Object Notation (JSON)
      • Connecting to MongoDB using Mongo
        • Saving information
        • Retrieving information
        • Deleting information
      • Exporting information using mongoexport
      • Importing data using mongoimport
      • Managing backup and restore using mongodump and mongorestore
      • Saving large files using mongofiles
      • bsondump
    • Installing Rails/Sinatra
    • Summary
    • Chapter 2: Diving Deep into MongoDB
      • Creating documents
      • Time for action – creating our first document
        • NoSQL scores over SQL databases
      • Using MongoDB embedded documents
      • Time for action – embedding reviews and votes
        • Fetching embedded objects
      • Using MongoDB document relationships
      • Time for action – creating document relations
      • Comparing MongoDB versus SQL syntax
      • Using Map/Reduce instead of join
        • Understanding functional programming
        • Building the map function
      • Time for action – writing the map function for calculating vote statistics
        • Building the reduce function
      • Time for action – writing the reduce function to process emitted information
      • Understanding the Ruby perspective
        • Setting up Rails and MongoDB
      • Time for action – creating the project
        • Understanding the Rails basics
        • Using Bundler
          • Why do we need the Bundler
        • Setting up Sodibee
      • Time for action – start your engines
        • Setting up Mongoid
    • Time for action – configuring Mongoid
      • Building the models
    • Time for action – planning the object schema
      • Testing from the Rails console
    • Time for action – putting it all together
      • Understanding many-to-many relationships in MongoDB
      • Using embedded documents
    • Time for action – adding reviews to books
      • Choosing whether to embed or not to embed
    • Time for action – embedding Lease and Purchase models
      • Working with Map/Reduce
    • Time for action – writing the map function to calculate ratings
    • Time for action – writing the reduce function to process the emitted results
      • Using Map/Reduce together
    • Time for action – working with Map/Reduce using Ruby
    • Summary
      • Chapter 3: MongoDB Internals
        • Understanding Binary JSON
          • Fetching and traversing data
          • Manipulating data
        • What is ObjectId?
        • Documents and collections
          • Capped collections
          • Dates in MongoDB
        • JavaScript and MongoDB
        • Time for action – writing our own custom functions in MongoDB
        • Ensuring write consistency or "read your writes"
          • How does MongoDB use its memory-mapped storage engine?
          • Advantages of write-ahead journaling
        • Global write lock
        • Transactional support in MongoDB
          • Understanding embedded documents and atomic updates
          • Implementing optimistic locking in MongoDB
        • Time for action – implementing optimistic locking
          • Choosing between ACID transactions and MongoDB transactions
        • Why are there no joins in MongoDB?
        • Summary
        • Chapter 4: Working Out Your Way with Queries
          • Searching by fields in a document
          • Time for action – searching by a string value
            • Querying for specific fields
          • Time for action – fetching only for specific fields
            • Using skip and limit
          • Time for action – skipping documents and limiting our search results
            • Writing conditional queries
              • Using the $or operator
          • Time for action – finding books by name or publisher
            • Writing threshold queries with $gt, $lt, $ne, $lte, and $gte
        • Time for action – finding the highly ranked books
          • Checking presence using $exists
        • Searching inside arrays
        • Time for action – searching inside reviews
          • Searching inside arrays using $in and $nin
          • Searching for exact matches using $all
        • Searching inside hashes
        • Searching inside embedded documents
        • Searching with regular expressions
        • Time for action – using regular expression searches
        • Summary
          • Chapter 5: Ruby DataMappers: Ruby and MongoDB Go Hand in Hand
            • Why do we need Ruby DataMappers
              • The mongo-ruby-driver
            • Time for action – using mongo gem
            • The Ruby DataMappers for MongoDB
              • MongoMapper
              • Mongoid
            • Setting up DataMappers
              • Configuring MongoMapper
            • Time for action – configuring MongoMapper
              • Configuring Mongoid
            • Time for action – setting up Mongoid
            • Creating, updating, and destroying documents
              • Defining fields using MongoMapper
              • Defining fields using Mongoid
              • Creating objects
            • Time for action – creating and updating objects
            • Using finder methods
              • Using find method
              • Using the first and last methods
              • Using the all method
            • Using MongoDB criteria
              • Executing conditional queries using where
            • Time for action – fetching using the where criterion
              • Revisiting limit, skip, and offset
            • Understanding model relationships
              • The one to many relation
            • Time for action – relating models
              • Using MongoMapper
              • Using Mongoid
            • The many-to-many relation
          • Time for action – categorizing books
            • MongoMapper
            • Mongoid
            • Accessing many-to-many with MongoMapper
            • Accessing many-to-many relations using Mongoid
          • The one-to-one relation
            • Using MongoMapper
            • Using Mongoid
          • Time for action – adding book details
            • Understanding polymorphic relations
              • Implementing polymorphic relations the wrong way
              • Implementing polymorphic relations the correct way
          • Time for action – managing the driver entities
          • Time for action – creating vehicles using basic polymorphism
            • Choosing SCI or basic polymorphism
          • Using embedded objects
          • Time for action – creating embedded objects
            • Using MongoMapper
            • Using Mongoid
            • Using MongoMapper
            • Using Mongoid
          • 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
            • Single Collection Inheritance
          • Time for action – adding licenses to drivers
            • Basic embedded polymorphism
          • Time for action – insuring drivers
          • Choosing whether to embed or to associate documents
          • Mongoid or MongoMapper – the verdict
          • Summary
            • Chapter 6: Modeling Ruby with Mongoid
              • Developing a web application with Mongoid
                • Setting up Rails
              • Time for action – setting up a Rails project
                • Setting up Sinatra
              • Time for action – using Sinatra professionally
                • Understanding Rack
              • Defining attributes in models
                • Accessing attributes
                • Indexing attributes
                  • Unique indexes
                  • Background indexing
                  • Geospatial indexing
                  • Sparse indexing
                • Dynamic fields
              • Time for action – adding dynamic fields
                • Localization
              • Time for action – localizing fields
              • Using arrays and hashes in models
                • Embedded objects
              • Defining relations in models
                • Common options for all relations
                  • :class_name option
                  • :inverse_of option
                  • :name option
                • Relation-specific options
                • Options for has_one
                  • :as option
                  • :autosave option
                  • :dependent option
                  • :foreign_key option
                • Options for has_many
                  • :order option
                • Options for belongs_to
                  • :index option
                  • :polymorphic option
                • Options for has_and_belongs_to_many
                  • :inverse_of option
              • Time for action – configuring the many-to-many relation
              • Time for action – setting up the following and followers relationship
                • Options for :embeds_one
                  • :cascade_callbacks option
                  • :cyclic
              • Time for action – setting up cyclic relations
                • Options for embeds_many
                  • :versioned option
                • Options for embedded_in
                  • :name option
              • Managing changes in models
              • Time for action – changing models
              • Mixing in Mongoid modules
                • The Paranoia module
              • Time for action – getting paranoid
                • Versioning
              • Time for action – including a version
              • Summary
              • Chapter 7: 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
                  • Using mongostat
                • Understanding web application performance
                  • Web server response time
                  • Throughput
                    • Load the server using httperf
                    • Monitoring server performance
                  • End-user response and latency
                • Optimizing our code for performance
                  • Indexing fields
                  • Optimizing data selection
                • Optimizing and tuning the web application stack
                  • Performance of the memory-mapped storage engine
                  • Choosing the Ruby application server
                    • Passenger
                    • Mongrel and Thin
                    • Unicorn
                  • Increasing performance of Mongoid using bson_ext gem
                  • Caching objects
                    • Memcache
                    • Redis server
                • Summary
                • Chapter 8: Rack, Sinatra, Rails, and MongoDB – Making Use of them All
                  • Revisiting Sodibee
                  • The Rails way
                    • Setting up the project
                    • Modeling Sodibee
                  • Time for action – modeling the Author class
                  • Time for action – writing the Book, Category and Address models
                  • Time for action – modeling the Order class
                    • Understanding Rails routes
                    • What is the RESTful interface?
                  • Time for action – configuring routes
                    • Understanding the Rails architecture
                    • Processing a Rails request
                    • Coding the Controllers and the Views
                  • Time for action – writing the AuthorsController
                    • Solving the N+1 query problem using the includes method
                    • Relating models without persisting them
                  • Designing the web application layout
                • Time for action – designing the layout
                  • Understanding the Rails asset pipeline
                • Designing the Authors listing page
                • Time for action – listing authors
                  • Adding new authors and their books
                • Time for action – adding new authors and books
                • The Sinatra way
                • Time for action – setting up Sinatra and Rack
                • Testing and automation using RSpec
                  • Understanding RSpec
                • Time for action – installing RSpec
                • Time for action – sporking it
                • Documenting code using YARD
                • Summary
                  • Chapter 9: Going Everywhere – Geospatial Indexing with MongoDB
                    • What is geolocation
                      • How accurate is a geolocation
                      • Converting geolocation to geocoded coordinates
                    • Identifying the exact geolocation
                    • Storing coordinates in MongoDB
                    • Time for action – geocoding the Address model
                      • Testing geolocation storage
                    • Time for action – saving geolocation coordinates
                      • Using geocoder to update coordinates
                    • Time for action – using geocoder for storing coordinates
                    • Firing geolocation queries
                    • Time for action – finding nearby addresses
                      • Using mongoid_spacial
                    • Time for action – firing near queries in Mongoid
                      • Differences between $near and $geoNear
                  • Summary
                    • Chapter 10: Scaling MongoDB
                      • High availability and failover via replication
                        • Implementing the master/slave replication
                      • Time for action – setting up the master/slave replication
                        • Using replica sets
                      • Time for action – implementing replica sets
                        • Recovering from crashes – failover
                        • Adding members to the replica set
                      • Implementing replica sets for Sodibee
                      • Time for action – configuring replica sets for Sodibee
                      • Implementing sharding
                        • Creating the shards
                      • Time for action – setting up the shards
                        • Configuring the shards with a config server
                      • Time for action – starting the config server
                        • Setting up the routing service – mongos
                      • Time for action – setting up mongos
                        • Testing shared replication
                      • 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
                        • Performance benchmarking
                      • Time for action – iterating Ruby objects
                      • Summary

                      Gautam Rege

                      Gautam Rege has been working in Ruby since 2007 and has been experimenting a lot with MongoDB. He believes Ruby is indeed the language of the next generation – very human-readable, flexible and extremely enjoyable. MongoDB is the new age NoSQL forerunner. It has rightly started to gain ‘humongous’ patronage. Gautam works a lot in Rails and at Josh Software Private Limited, the company he co-founded, working exclusively in Ruby and Ruby-related technologies. He is well versed with Rhodes (the Ruby mobile framework), Merb, Sinatra and occasionally also experiments with JS frameworks like Sproutcore, and Backbone.js
                      Sorry, we don't have any reviews for this title yet.

                      Code Downloads

                      Download the code and support files for this book.


                      Submit Errata

                      Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.

                      Sample chapters

                      You can view our sample chapters and prefaces of this title on PacktLib or download sample chapters in PDF format.

                      Frequently bought together

                      Ruby and MongoDB Web Development Beginner's Guide +    TYPO3: Enterprise Content Management =
                      50% Off
                      the second eBook
                      Price for both: $31.50

                      Buy both these recommended eBooks together and get 50% off the cheapest eBook.

                      What you will learn from this book

                      • Get MongoDB and Ruby up and running on your machine (Windows/Mac/Linux)
                      • Leverage MongoDB to build highly scalable data-centric applications
                      • Leverage MongoDB replication to ensure high availability and reliability
                      • Create your first object and save it to MongoDB
                      • Create a Ruby Object and map it to MongoDB document
                      • Process large datasets with MapReduce
                      • Create geo-spatial indexes or 2D indexes
                      • Learn about Mongoid and MongoMapper for mapping Ruby objects to MongoDB documents

                      In Detail

                      MongoDB is a high-performance, open source, schema-free document-oriented database. Ruby is an object- oriented scripting language. Ruby and MongoDB are an ideal partnership for building scalable web applications.

                      Ruby and MongoDB Web Development Beginner's Guide is a fast-paced, hands-on guide to get started with web application development using Ruby and MongoDB. The book follows a practical approach, using clear and step-by-step instructions and examples in Ruby to demonstrate application development using MongoDB.

                      The book starts by introducing the concepts of MongoDB. The book teaches everything right from the installation to creating objects, MongoDB internals, queries and Ruby Data Mappers.

                      You will learn how to use various Ruby data mappers like Mongoid and MongoMapper to map Ruby objects to MongoDB documents.

                      You will learn MongoDB features and deal with geo-spatial indexing with MongoDB and Scaling MongoDB.

                      With its coverage of concepts and practical examples, Ruby and MongoDB Web Development Beginner's Guide is the right choice for Ruby developers to get started with developing websites with MongoDB as the database.

                      Approach

                      This book is packed with step-by-step instructions and practical examples, along with accompanying screenshots and code for an easy learning curve. The needs of a beginner are totally satisfied by the book.

                      Who this book is for

                      This book assumes that you are experienced in Ruby development skills - Ruby, HTML, CSS. Having knowledge of using NoSQL will help you get through the concepts quicker, but it is not mandatory. No prior knowledge of MongoDB required.

                      Code Download and Errata
                      Packt Anytime, Anywhere
                      Register Books
                      Print Upgrades
                      eBook Downloads
                      Video Support
                      Contact Us
                      Awards Voting Nominations Previous Winners
                      Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
                      Resources
                      Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software