On Github dpzaba / mongodb-slides
by David Zaba
dpzaba@gmail.com
dpzaba.com
CiudadReal-rb Agile-CR
sudo apt-get install mongodb # official repositories: # mongodb-10gen
david:~$ mongo MongoDB shell version: 2.4.9 connecting to: test >try.mongodb.org
{ _id: ObjectID("509a8fb2f3f4948bd2f983a0"), // 12 bytes: timestamp + m_id + p_id + counter name: "David", age: 24, status: "A" }
db.users.insert( { name: "David", age: 24, status: "A" } ) db.users.save( { name: "David", age: 24, status: "A" } )
db.users.find( { status: "A" }, { name: 1, status: 1, _id: 0 } ) db.users.find( { name: /David.*/i } )
db.users.find( { age: { $gt: 21 } } ) db.users.find( { $or: [ { status: "A" }, { age: 24 } ] } )
db.users.update( { age: { $gt: 25 } }, { $set: { status: "C" } }, { upsert: true, multi: true } ) db.users.save( { _id: 7, user_id: "David", age: 24, status: "A" } )
db.users.remove( { status: "D" }, true ) //justOne
/* Exam */ { _id: 1, subject: "Redes", questions: [ 5, 7, 9] } /* Question */ { _id: 5, puctuation: 1.5, question_text: "Diferencias entre TCP y UDP" }
/* Exam */ { _id: 1, subject: "Redes", questions: [ { punctuation: 1.5, question_text: "Diferencias entre TCP y UDP" }, { punctuation: 1, question_text: "¿Qué es un enrutador IP?" }, ... ] }
# Conex ... # collection users users = db["users"] users.count users.insert(:name => "David") users.find({_id: 7}, :fields => ["name", "email"]) users.find.each { |user| puts user.inspect } users.update({_id: 7}, {:$set => { :name => "Zaba" }}) users.remove(:name => "David")
class User include MongoMapper::Document key :name, String key :age, Integer end # User.create(:name => "David") User.where(id: 7).fields(:name,:email)
class Exam include MongoMapper::Document key :subject, String many :questions end class Question include MongoMapper::Document # include MongoMapper::EmbeddedDocument key :question_text, String key :punctuation, Integer belongs_to :exam end