Skip to content

Instantly share code, notes, and snippets.

@kidpollo
Created November 12, 2009 00:24
Show Gist options
  • Save kidpollo/232463 to your computer and use it in GitHub Desktop.
Save kidpollo/232463 to your computer and use it in GitHub Desktop.
########### pio.la
# => Ver Configuracion
# cat /usr/local/mongodb/mongod.conf
# => Iniciar mongo
# sudo mongod --config /usr/local/mongodb/mongod.conf
require "mongomapper"
MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017)
MongoMapper.database = 'piola'
MongoMapper.connection.database_names
MongoMapper.connection.drop_database('piola')
MongoMapper.connection.database_names
MongoMapper.database = 'piola'
MongoMapper.connection.database_names
@db = MongoMapper.database
#se puede administrar la base de datos desde el driver!
Mongo::Admin.new(@db).profiling_level = :all
class Person
include MongoMapper::Document
key :first_name, String
key :last_name, String
key :age, Integer
key :born_at, Time
key :active, Boolean
key :fav_colors, Array
end
person = Person.create({
:first_name => 'Paco',
:last_name => 'Viramontes',
:age => 25,
:born_at => Time.mktime(1983, 12, 03, 10, 45),
:active => true,
:fav_colors => %w(red green blue)
})
person.first_name = 'Paki'
person.fav_colors = %w(verde)
person.save
person.destroy
Person.validates_presence_of :first_name
Person.validates_presence_of :last_name
Person.validates_numericality_of :age
#falla
person = Person.create({
:born_at => Time.mktime(1983, 12, 03, 10, 45),
:active => true,
:fav_colors => %w(red green blue)
})
paco = Person.create({
:first_name => 'Paco',
:last_name => 'Viramontes',
:age => 27,
:born_at => Time.mktime(1983, 12, 03, 10, 45),
:active => true,
:fav_colors => %w(red green blue)
})
class Address
include MongoMapper::EmbeddedDocument
key :address, String
key :city, String
key :state, String
key :zip, Integer
end
Person.many :addresses
paki = Person.last
paki.addresses << Address.new(:city => 'Guadalajara', :state => 'JAL')
paki.addresses << Address.new(:city => 'San Luis Potosi', :state => 'SLP')
paki.save
Person.last.addresses
Person.all(:conditions => {'addresses.city' => 'Guadalajara'})
Person.all(:first_name => "Paco", :date_time=>{"$gte"=> Time.mktime(1983, 12, 03, 10, 45), "$lte" => Time.now })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment