Book Image

Ruby and MongoDB Web Development Beginner's Guide

By : Gautam Rege
Book Image

Ruby and MongoDB Web Development Beginner's Guide

By: Gautam Rege

Overview of this book

<p>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.<br /><br /><em>Ruby and MongoDB Web Development Beginner's Guide</em> 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. <br /><br />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. <br /><br />You will learn how to use various Ruby data mappers like Mongoid and MongoMapper to map Ruby objects to MongoDB documents.<br /><br />You will learn MongoDB features and deal with geo-spatial indexing with MongoDB and Scaling MongoDB. <br /><br />With its coverage of concepts and practical examples, <em>Ruby and MongoDB Web Development Beginner's Guide</em> is the right choice for Ruby developers to get started with developing websites with MongoDB as the database.</p>
Table of Contents (18 chapters)
Ruby and MongoDB Web Development Beginner's Guide
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface

Time for action — geocoding the Address model


As the Address is a model for storing the location, we can use it for geospatial indexing! This is done as follows:

class Address
include Mongoid::Document
field :street, type: String
field :zip, type: Integer
field :city, type: String
field :state, type: String
field :country, type: String
field :coordinates, type: Array
index [[ :coordinates, Mongo::GEO2D ]]
belongs_to :location, polymorphic: true
end

The indexes need to be created in the model manually. Mongoid will not issue commands to create them unless explicitly told to do so. Let's create indexes as follows:

$ rake db:mongoid:create_indexes
Generated indexes for Address
Generated indexes for Author
Generated indexes for Book
Generated indexes for Category
Not a Mongoid parent model: app/models/lease.rb
Generated indexes for Member
Generated indexes for Order
Not a Mongoid parent model: app/models/purchase.rb
Not a Mongoid parent model: app/models/review.rb

What just happened?

MongoDB...