Created
February 19, 2010 05:51
-
-
Save alanrrb/308473 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'rubygems' | |
| require 'mongo' | |
| conn = Mongo::Connection.new | |
| db = conn.db("meu_bd") | |
| coll_usuarios = db.collection("usuarios") | |
| coll_usuarios.insert({:nome => "Alan", | |
| :email => "[email protected]", | |
| :nickname => "alanrrb"}) | |
| coll_usuarios.insert({:nome => "Jose", | |
| :email => "[email protected]", | |
| :nickname => "jose123321"}) | |
| coll_usuarios.insert({:nome => "Tom", | |
| :email => "[email protected]", | |
| :nickname => "tom321123"}) | |
| usuario = coll_usuarios.find_one() | |
| p usuario | |
| coll_usuarios.find(:nome => "Tom").each do |doc| | |
| p doc | |
| end | |
| require 'mongo_mapper' | |
| MongoMapper.database = "meu_bd" | |
| class Usuario | |
| include MongoMapper::Document | |
| key :nome, String | |
| key :email, String | |
| key :nickname, String | |
| end | |
| marcia = Usuario.new | |
| marcia.nome = "Marcia" | |
| marcia.email = "[email protected]" | |
| marcia.nickname = "nick_marcia" | |
| marcia.save | |
| #Adicionando Relacionamentos | |
| class Usuario | |
| include MongoMapper::Document | |
| many :pagamentos | |
| end | |
| class Pagamento | |
| include MongoMapper::Document | |
| key :valor, Float | |
| belongs_to :usuario | |
| end | |
| marcia.pagamentos << Pagamento.new(:valor => 1.15) | |
| marcia.save | |
| alan = Usuario.find_by_nome("Alan") | |
| p alan | |
| david = Usuario.create(:nome => "David", :email => "[email protected]", :nickname => "david") | |
| p david |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment