Mongo+mongo mapper

29
Mongo + MongoMapper Daniel Quirino Oliveira [email protected] Thursday, April 15, 2010

Transcript of Mongo+mongo mapper

Page 1: Mongo+mongo mapper

Mongo + MongoMapperDaniel Quirino Oliveira

[email protected]

Thursday, April 15, 2010

Page 2: Mongo+mongo mapper

For those of you who may have lost the first talk:

http://nullability.org/blog/?p=11http://nullability.org/blog/?p=21

Thursday, April 15, 2010

Page 3: Mongo+mongo mapper

Collections ≈ TablesDocuments ≈ Tuples

Attributes ≈ Columns

Thursday, April 15, 2010

Page 4: Mongo+mongo mapper

Relationships

Thursday, April 15, 2010

Page 5: Mongo+mongo mapper

1-1

Mongocontact = {_id: ObjectId(“3eab2389b1”),name: “John”,email: “[email protected],address: ObjectId(“4d70923ff2”)};

address = {_id: ObjectId(“4d70923ff2”),city: “Sao Paulo”,country: “Brazil”}

MongoMapperclass Contact include MongoMapper::Document key :name key :email key :address_id, ObjectIdend

class Address include MongoMapper::Document key :city key :countryend

Thursday, April 15, 2010

Page 6: Mongo+mongo mapper

1-n

Mongocontact = {_id: ObjectId(“3eab2389b1”),name: “John”,email: “[email protected]};

address1 = { "_id" : ObjectId("4bc7510cddcb"), "city" : "Araraquara", "contact_id" : ObjectId("3eab2389b1")};address2 = { "_id" : ObjectId("4bc75110ddc"), "city" : "Sao Carlos", "contact_id" : ObjectId("3eab2389b1") };

MongoMapperclass Contact include MongoMapper::Document key :name key :email many :addressesend

class Address include MongoMapper::Document key :city key :countryend

Thursday, April 15, 2010

Page 7: Mongo+mongo mapper

1-n (embedded)Mongo

contact = { “_id”: ObjectId(“3eab2389b1”), “name”: “John”, “email”: “[email protected], “addresses”:[ { "_id" : ObjectId("4bc7510cddcb"), "city" : "Campinas"}, { "_id" : ObjectId("4bc75110ddc"), "city" : "Sorocaba"} ]}

MongoMapperclass Contact include MongoMapper::Document key :name key :email many :addressesend

class Address include MongoMapper::EmbeddedDocument key :city key :countryend

Thursday, April 15, 2010

Page 8: Mongo+mongo mapper

More on http://www.mongodb.org/display/DOCS/MongoDB+Data+Modeling+and+Rails

Thursday, April 15, 2010

Page 9: Mongo+mongo mapper

Sorting and Indexes

Thursday, April 15, 2010

Page 10: Mongo+mongo mapper

Index is B-tree used to quickly sort documents in a collection

Thursday, April 15, 2010

Page 11: Mongo+mongo mapper

Default index created on _id

Thursday, April 15, 2010

Page 12: Mongo+mongo mapper

But, what if I want to order my documents by, hmmm..., name

attribute?

Thursday, April 15, 2010

Page 13: Mongo+mongo mapper

db.coll_name.ensureIndex({name:1});sorts ascendingly

Thursday, April 15, 2010

Page 14: Mongo+mongo mapper

db.coll_name.ensureIndex({name:-1});sorts descendingly

Thursday, April 15, 2010

Page 15: Mongo+mongo mapper

db.coll_name.ensureIndex({name:1, email:-1});sorts name ascendingly and email descendingly

Thursday, April 15, 2010

Page 16: Mongo+mongo mapper

db.coll_name.ensureIndex({name:1}, {unique:true});sorts name ascendingly and name is unique

Thursday, April 15, 2010

Page 17: Mongo+mongo mapper

db.coll_name.find().sort({name:1});// select * from ... order by name ASC

db.coll_name.find().sort({name:-1});// select * from ... order by name DESC

Thursday, April 15, 2010

Page 18: Mongo+mongo mapper

class Contact include MongoMapper::Document key :name, :index => true # or :unique => trueend

# config/initializer/mongo.rbinclude MongoMapper

MongoMapper.connection = Mongo::Connection.new('localhost')MongoMapper.database = "mongo_demo_#{Rails.env}"

Dir[Rails.root + 'app/models/**/*.rb'].each do |model_path| File.basename(model_path, '.rb').classify.constantizeend

# ensure all needed indexes are createdMongoMapper.ensure_indexes!

Thursday, April 15, 2010

Page 19: Mongo+mongo mapper

More on http://www.mongodb.org/display/DOCS/Indexes

Thursday, April 15, 2010

Page 20: Mongo+mongo mapper

Queries

Thursday, April 15, 2010

Page 21: Mongo+mongo mapper

db.coll.find() // select * from...db.coll.find({“name”: “Daniel”}) // where name = ‘Daniel’db.coll.find({“name”: “Daniel”, “age”: 27}) // and age = 27db.coll.find({“name”: “Daniel”, “age”: {$gte:20}}) // and age >= 20db.coll.find({“name”: {$ne: “Daniel”}}) // name != “Daniel”db.coll.find({“name”: /daniel.*/i}) // like “daniel%”db.coll.find({“addresses”: {$elemMatch : {“city” : “Campinas”}}})

Thursday, April 15, 2010

Page 22: Mongo+mongo mapper

MongoMapper has dynamic finders

Thursday, April 15, 2010

Page 23: Mongo+mongo mapper

Contact.all()Contact.find_all_by_name(‘Daniel’)Contact.find_all_by_name(/j.*/i)Contact.all(:conditions => {:name.ne => ‘Daniel’})Contact.all(:conditions => {:name => ‘Daniel’, :age => 20})Contact.all(:conditions => {:name => ‘Daniel’, :age.gte => 27})Contact.all(:conditions => {:name => ‘Daniel’, :age.gte => 27})Contact.all(:conditions => {:name => ‘Daniel’, :order => ‘email DESC’})

Thursday, April 15, 2010

Page 24: Mongo+mongo mapper

More on http://www.mongodb.org/display/DOCS/Advanced+Queries

Thursday, April 15, 2010

Page 25: Mongo+mongo mapper

Attachments?

Thursday, April 15, 2010

Page 26: Mongo+mongo mapper

Griphttp://github.com/twoism/grip

Thursday, April 15, 2010

Page 27: Mongo+mongo mapper

MongoMapper + Gripclass Contact include MongoMapper::Document include Grip::HasAttachment key :name key :email has_grid_attachment :photo many :addressesend

contact.photo = File.open(‘/Users/daniel/Pictures/my_pic.jpg’)contact.save

Thursday, April 15, 2010

Page 28: Mongo+mongo mapper

Q&A?

Thursday, April 15, 2010

Page 29: Mongo+mongo mapper

Danke :)

Thursday, April 15, 2010