Skip to content

Latest commit

 

History

History
133 lines (103 loc) · 2.8 KB

mongo.md

File metadata and controls

133 lines (103 loc) · 2.8 KB

Mongo

There are many ORMs out there for Mongo (or ODMs in this case). This page will go over just a few.

Require the Mongo gem in your app:

require 'rubygems'
require 'sinatra'
require 'mongo'
###[MongoMapper][mongomapper]### require 'mongomapper'

Create the Model class

class Link
  include MongoMapper::Document
  key :title, String
  key :link, String
end

Create the route:

get '/' do
 @links = Link.all
 haml :links
end

(top)

###[Mongoid][mongoid]### require 'mongoid'

Create the Model class

class Link
  include Mongoid::Document
  field :title, :type => String
  field :link, :type => String
end

Create the route:

get '/' do
 @links = Link.all
 haml :links
end

(top)

###[Candy][candy]### require 'candy'

Create the Model class

class Link
  include Candy::Piece
end

class Links
  include Candy::Collection
  collects :link   # Declares the Mongo collection is 'Link'
end

Link.connection # => Defaults to localhost port 27017
Link.db         # => Defaults to your username, or 'candy' if unknown
Link.collection # => Defaults to the class name ('Link')

Create the route:

get '/' do
 @links = Links.all
 haml :links
end

(top)

###[Mongomatic][mongomatic]### require 'mongomatic'

Create the Model class

class Link < Mongomatic::Base
  def validate
    self.errors.add "title", "blank" if self["title"].blank?
    self.errors.add "link",  "blank" if self["link"].blank?
  end
end

Create the route:

get '/' do
 @links = Link.all
 haml :links
end

def validate
  self.errors.add "name", "blank" if self["name"].blank?
  self.errors.add "email", "blank" if self["email"].blank?
  self.errors.add "address.zip", "blank" if (self["address"] || {})["zip"].blank?
end

(top)

###[MongoODM][mongoodm]### require 'mongo_odm'

Create the Model class

class Link
  include MongoODM::Document
  field :title
  field :link
end

Create the route:

get '/' do
 @links = Link.find.to_a
 haml :links
end

(top)