Ruby on Rails - Mongodb
Using MongoDB with Ruby on Rails
- Fast in-place updates with atomic modifiers
- Ruby on Rails is productive!
- Object Mappers available for MongoDB
- Mongo Ruby Driver
Mobile / Web
- Ruby on Rails makes REST easy, making APIs easier.
- Render responses as text, JSON, or XML
- Geospatial indexing for location based queries.
- Location-centric websites and mobile applications.
Why we chose MongoDB
- Cowrite - collaborative writing web application
- Versioning needed
- Originally using GridFS
- Travel720 - Gift registry web site
- Self contained relations can take advantage of embedding documents
Mongo Object Mappers for Ruby
- MongoMapper
- Mongoid
- Mongo ODM
- MongoModel
Why we chose Mongoid
- Excellent documentation
- Active community
- Compatibility with other projects/gems
- Similar API to ActiveRecord
- Uses ActiveValidation
- Mongoid Extras: Caching, Paranoid Documents, Versioning, Timestamping,
- Composite Keys
Compatible Gems with Mongoid
- Devise - Authentication solution for Rails based on Warden. Supports
- Mongoid out of box.
- Carrierwave - simple and flexible way to upload files from Ruby Applications.
- Supports grid_fs.
- Geocoder - complete geocoding solution for Rails. Adds geocoding by street
- or IP address, reverse geocoding, and distance queries.
- Mongoid-rspec - RSpec matchers and macros for Mongoid.
Learn Ruby on Rails - Ruby on Rails tutorial - Mongoid example - Ruby on Rails examples - Ruby On Rails programs
Learn Ruby on Rails - Ruby on Rails tutorial - Mongoid program - Ruby on Rails examples - Ruby On Rails programs
Developing with MongoDB/Mongoid
- Generating models is the same using the console as with ActiveRecord
- rails generate model Team name:string city:string
- location:array
- No migrations needed!
- Take advantage of embedded documents in models where applicable for
- increased performance.
- Store large files using GridFS
Fields available for Mongoid
- Array
- BigDecimal (Stored as a String)
- Boolean
- Date
- DateTime
- Float
- Hash
- Integer
- Range
- String
- Symbol
- Time
Learn Ruby on Rails - Ruby on Rails tutorial - Mongoid Document - Ruby on Rails examples - Ruby On Rails programs
Persisting in the Controller
Learn Ruby on Rails - Ruby on Rails tutorial - Persisting controller - Ruby on Rails examples - Ruby On Rails programs
Indexing
Learn Ruby on Rails - Ruby on Rails tutorial - Indexing - Ruby on Rails examples - Ruby On Rails programs
- To create indexes in the database use the rake task
- Or configure to autocreate in mongoid.yml (not recommended)
Relations in Models
- Associations between models can be embedded or referenced
- NO JOINS!
- Objects that are referenced involve a separate query
- Embedded documents can be very efficient with reducing queries to one
- while managing the size of the document
- One to One, One to Many, and Many to Many relations available
Relations in Mongoid - Embedded
Learn Ruby on Rails - Ruby on Rails tutorial - Embedded Relations - Ruby on Rails examples - Ruby On Rails programs
Embedded Posts
Learn Ruby on Rails - Ruby on Rails tutorial - Embedded Posts - Ruby on Rails examples - Ruby On Rails programs
Polymorphic Behavior
Learn Ruby on Rails - Ruby on Rails tutorial - polymorphic behaviour - Ruby on Rails examples - Ruby On Rails programs
Relations in Mongoid - Referenced
Learn Ruby on Rails - Ruby on Rails tutorial - relations in mongoid - Ruby on Rails examples - Ruby On Rails programs
Learn Ruby on Rails - Ruby on Rails tutorial - Many to Many Relationship - Ruby on Rails examples - Ruby On Rails programs
- Queries are of type Criteria, which is a chainable and lazily evaluated wrapper to a MongoDB dynamic query. Chainable queries include:
- all_in
- all_of
- also_in
- and
- any_of
- asc
- desc
- distinct
- excludes
- includes
- limit
- near
- not_in
- only
- order_by
- skip
- where
- without
Query Examples
Versioning with Mongoid
- Embeds a version of the object on each save.
- Can skip versioning and also set the max versions.
- Add to model
Testing Rails with MongoDB
- RSpec-Mongoid provides test matchers
- RSpec does not refresh the database with each test run.
- Database Cleaner to the rescue. It be setup to truncate a database before
- running tests. Add below after installing the DatabaseCleaner Gem.
Hosting Options
- Heroku is a widely used cloud hosting for Ruby on Rails
- MongoHQ and MongoLab both have add on options
- Options and pricing are similar