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

Creating, updating, and destroying documents


Now let's work with objects—creating, updating, and deleting them. But first, we need to set up the model with attributes. We add these attributes in the models directly. Each attribute has a name and also specifies the type of data storage. To ensure we see all the standard data types, we shall see the Person model.

Defining fields using MongoMapper

We define the model in the app/models/person.rb file as follows:

class Person
include MongoMapper::Document
key :name, String
key :age, Integer
key :height, Float
key :born_on, Date
key :born_at, Time
key :interests, Array
key :is_alive, Boolean
end

Defining fields using Mongoid

With Mongoid, there is just a difference in syntax:

class Person
include Mongoid::Document
field :name, type: String
field :age, type: Integer
field :height, type: Float
field :born_on, type: Date
field :born_at, type: Time
field :interests, type: Array
field :is_alive, type: Boolean
end

Creating objects

The way to create...