Mongo+mongo mapper
Upcoming SlideShare
Loading in...5
×
 

Mongo+mongo mapper

on

  • 5,805 views

 

Statistics

Views

Total Views
5,805
Views on SlideShare
5,633
Embed Views
172

Actions

Likes
6
Downloads
31
Comments
1

3 Embeds 172

http://nullability.org 106
http://www.slideshare.net 65
http://www.locawebers.com.br 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • Thanks Daniel, very helpful.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Mongo+mongo mapper Mongo+mongo mapper Presentation Transcript

  • Mongo + MongoMapper Daniel 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=11 http://nullability.org/blog/?p=21 Thursday, April 15, 2010
  • Collections ≈ Tables Documents ≈ Tuples Attributes ≈ Columns Thursday, April 15, 2010 View slide
  • Relationships Thursday, April 15, 2010 View slide
  • 1-1 MongoMapper class Contact Mongo include MongoMapper::Document contact = {_id: ObjectId(“3eab2389b1”), key :name name: “John”, key :email email: “john@email.com, key :address_id, ObjectId address: ObjectId(“4d70923ff2”)}; end address = {_id: ObjectId(“4d70923ff2”), class Address city: “Sao Paulo”, include MongoMapper::Document country: “Brazil”} key :city key :country end Thursday, April 15, 2010
  • 1-n MongoMapper Mongo class Contact contact = {_id: ObjectId(“3eab2389b1”), include MongoMapper::Document name: “John”, key :name email: “john@email.com}; key :email many :addresses address1 = { "_id" : ObjectId("4bc7510cddcb"), end "city" : "Araraquara", "contact_id" : ObjectId("3eab2389b1")}; class Address address2 = { "_id" : ObjectId("4bc75110ddc"), include MongoMapper::Document "city" : "Sao Carlos", key :city "contact_id" : ObjectId("3eab2389b1") }; key :country end Thursday, April 15, 2010
  • 1-n (embedded) MongoMapper Mongo class Contact contact = { include MongoMapper::Document “_id”: ObjectId(“3eab2389b1”), key :name “name”: “John”, key :email “email”: “john@email.com, many :addresses “addresses”:[ end { "_id" : ObjectId("4bc7510cddcb"), "city" : "Campinas"}, class Address { "_id" : ObjectId("4bc75110ddc"), include MongoMapper::EmbeddedDocument "city" : "Sorocaba"} key :city ] key :country } end 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 => true end # config/initializer/mongo.rb include 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.constantize end # ensure all needed indexes are created MongoMapper.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 = 27 db.coll.find({“name”: “Daniel”, “age”: {$gte:20}}) // and age >= 20 db.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
  • Grip http://github.com/twoism/grip Thursday, April 15, 2010
  • MongoMapper + Grip class Contact include MongoMapper::Document include Grip::HasAttachment key :name key :email has_grid_attachment :photo many :addresses end 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