Mongo+mongo mapper

Post on 06-May-2015

4.259 views 2 download

Transcript of Mongo+mongo mapper

Mongo + MongoMapperDaniel Quirino Oliveira

daniel@nullability.org

Thursday, April 15, 2010

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

Collections ≈ TablesDocuments ≈ Tuples

Attributes ≈ Columns

Thursday, April 15, 2010

Relationships

Thursday, April 15, 2010

1-1

Mongocontact = {_id: ObjectId(“3eab2389b1”),name: “John”,email: “john@email.com,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

1-n

Mongocontact = {_id: ObjectId(“3eab2389b1”),name: “John”,email: “john@email.com};

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

1-n (embedded)Mongo

contact = { “_id”: ObjectId(“3eab2389b1”), “name”: “John”, “email”: “john@email.com, “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

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

Thursday, April 15, 2010

Sorting and Indexes

Thursday, April 15, 2010

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

Thursday, April 15, 2010

Default index created on _id

Thursday, April 15, 2010

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

attribute?

Thursday, April 15, 2010

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

Thursday, April 15, 2010

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

Thursday, April 15, 2010

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

Thursday, April 15, 2010

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

Thursday, April 15, 2010

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

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

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

Thursday, April 15, 2010

Queries

Thursday, April 15, 2010

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

MongoMapper has dynamic finders

Thursday, April 15, 2010

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

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

Thursday, April 15, 2010

Attachments?

Thursday, April 15, 2010

Griphttp://github.com/twoism/grip

Thursday, April 15, 2010

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

Q&A?

Thursday, April 15, 2010

Danke :)

Thursday, April 15, 2010