You are previewing Ruby and MongoDB Web Development Beginner's Guide.
O'Reilly logo
Ruby and MongoDB Web Development Beginner's Guide

Book Description

"

Create dynamic web applications by combining the power of Ruby and MongoDB with this book and ebook.

  • 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

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.

"

Table of Contents

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