require 'mongo/model'Basic example of working with [Mongo Model][mongodb_model].
In this example we’ll create simple model and examine basic CRUD and querying operations.
require 'mongo/model'Connecting to test database and cleaning it before starting.
Mongo::Model.default_database_name = :default_test
Mongo::Model.default_database.clearLet’s define Game Unit. Models are just plain Ruby Objects, there’s no any Attribute Scheme, Types, Proxies, or other complex stuff, just use standard Ruby practices.
class UnitInheriting our Unit Class from Mongo::Model (the inherit keyword is
just a simple shortcut including Module and its ClassMethods).
inherit Mongo::ModelYou can specify collection name explicitly or omit it and it will be guessed from the class name.
collection :unitsThere’s no need to define attributes, just use plain old Ruby technics to of working with objects.
attr_accessor :name, :status, :stats
def inspect; name end
endStats conaining statistics about Unit (it will be embedded into the Unit).
There are no difference between main and embedded objects, all of them are just standard Ruby objects.
class Unit::Stats
inherit Mongo::Model
attr_accessor :attack, :life, :shield
endLet’s create two great Heroes.
zeratul = Unit.new name: 'Zeratul', status: 'alive'
zeratul.stats = Unit::Stats.new attack: 85, life: 300, shield: 100
tassadar = Unit.new name: 'Tassadar', status: 'dead'
tassadar.stats = Unit::Stats.new attack: 0, life: 80, shield: 300Saving units to database
p zeratul.save # => true
p tassadar.save # => trueWe made error - mistakenly set Tassadar’s attack as zero, let’s fix it.
tassadar.stats.attack = 20
p tassadar.save # => trueQuerying, use standard MongoDB query.
p Unit.first(name: 'Zeratul') # => Zeratul
p Unit.all(name: 'Zeratul') # => [Zeratul]
Unit.all name: 'Zeratul' do |unit|
p unit # => Zeratul
endSimple dynamic finders (bang versions also availiable).
p Unit.by_name('Zeratul') # => Zeratul
p Unit.first_by_name('Zeratul') # => Zeratul
p Unit.all_by_name('Zeratul') # => [Zeratul]In this example we covered basics of Mongo Model, please go to contents for more samples.