26. No need to create the
collection
class Band
include Mongoid::Document
end
27. Store anything
class ParamsStore
include Mongoid::Document
end
class ApplicationController < ActionController::Base
before_filter do
Stuff.create(params)
end
end
28. Store long stuff
class Website
include Mongoid::Document
field :html, type: String
field :css, type: String
field :javascript, type: String
embeds_many :images
end
class Image
include Mongoid::Document
field :size, type: Integer
field :file, type: Moped::BSON::Binary
embedded_in :website
end
29. Geo
class Person
include Mongoid::Document
field :location, :type => Array
index(
{ location: Mongo::GEO2D },
{ min: -180, max: 180 }
)
end
coimbra_location = [20, -8]
Person.geo_near(coimbra_location).spherical
30. Full text search
class Document
include Mongoid::Document
field :content, :type => String
index(
{:content => 'text'},
{:default_language => 'portuguese’}
)
end
Document.text_search('IKEA')
31. Caveats (not so nice)
No joins
Impact of key length
No integrity checks for references
Document size limit of 16MB
Nesting depth limit of 100
34. Oauths
class User
include Mongoid::Document
embeds_many :oauths
index [["oauths.provider", Mongo::ASCENDING],
["oauths.uid", Mongo::ASCENDING]]
end
class Oauth
include Mongoid:Document
field :provider, type: String
field :token, type: String
field :secret, type: String
field :uid, type: String
embedded_in :user
end
35. Activity Stream
class Activity
include Mongoid::Document
include Mongoid::Timestamps
field :verb, :type => Symbol
field :actor, :type => Hash
field :object, :type => Hash
field :target, :type => Hash
field :receivers, :type => Array
embeds_many :likes
end
36. Relationships
class User
include Mongoid::Document
has_and_belongs_to_many :followers, :class_name => "User",
:inverse_of => :followed_users
end
{
_id: ObjectId("4d541f6823380f670d000008"),
follower_ids: [
ObjectId("4d4ea8ec86d56d2907000003"),
ObjectId("50fe17c226c1b7000200018a")
],
}
Need smarter relationships? Embed them!
good
- awesome for prototyping
- fast development
- freedom
- fast growing community
bad:
size of parameters
more responsability
not many tools
no native migrations, but there are alternatives
- not nearly as used as PostgreSQL or MySQL
- decisions decisions