SlideShare a Scribd company logo
Simple MongoDB design for
Web (Rails) apps
Sérgio Santos
@sdsantos
Improve Coimbra
Introduction / Disclamer
Bundlr MongoDB stats
MongoDB from the start
~ 3½ years of development
~ 6 GB of data
~ 6 million documents
Cloud hosted on MongoHQ
Burocracia MongoDB stats
~ 3 weekends of development
~ 300MB of data
~ 721 documents
~ 400KB per document
Cloud hosted on MongoLab
Why MongoDB?
MongoDB 101
Documents
Collections
References
Embeds
Rails MongoDB ORMs
Mongoid vs MongoMapper
Mongoid
Models
class Person
include Mongoid::Document
end
person = Person.new
person[:name] = 'Sérgio'
person[:age] = 26
person.save
Models
class Person
include Mongoid::Document
field :name, type: String
field :age, type: Integer
end
person = Person.new
person.name = 'Sérgio'
person.age = 26
person.save
Persistence
person = Person.create(name: 'Sérgio', age: 26)
person.update_attributes(name: 'Sérgio Santos')
person.touch
person.delete
person.destroy
person.rename(:name, :first_name)
Querying
person = Person.find("4baa56f1230048567300485c")
people = Person.where(age: 18)
people = Person.where(name: 'Sérgio').not(age: 26)
Person.count
Person.all.avg(:age)
-- INSERT MAP/REDUCE CLEVER EXAMPLE HERE --
References
class Band
include Mongoid::Document
has_many :members
end
class Member
include Mongoid::Document
field :name, type: String
belongs_to :band
end
References
# The parent band document.
{
"_id" : ObjectId("4d3ed089fb60ab534684b7e9")
}
# The child member document.
{
"_id" : ObjectId("4d3ed089fb60ab534684b7f1"),
"name" : "Matt Berninger"
"band_id" : ObjectId("4d3ed089fb60ab534684b7e9")
}
Embeds
class Band
include Mongoid::Document
embeds_many :albums
end
class Album
include Mongoid::Document
field :name, type: String
embedded_in :band
end
Embeds
{
"_id" : ObjectId("4d3ed089fb60ab534684b7e9"),
"albums" : [
{
"_id" : ObjectId("4d3ed089fb60ab534684b7e0"),
"name" : "Boxer",
}
]
}
Extras
timestamps
identity map & cache
paranoia
versioning
Cloud Hosting
The Nice Stuff
No need to create the
collection
class Band
include Mongoid::Document
end
Store anything
class ParamsStore
include Mongoid::Document
end
class ApplicationController < ActionController::Base
before_filter do
Stuff.create(params)
end
end
Store long stuff
class Website
include Mongoid::Document
field :html, type: String
field :css, type: String
field :javascript, type: String
embeds_many :images
end
class Image
include Mongoid::Document
field :size, type: Integer
field :file, type: Moped::BSON::Binary
embedded_in :website
end
Geo
class Person
include Mongoid::Document
field :location, :type => Array
index(
{ location: Mongo::GEO2D },
{ min: -180, max: 180 }
)
end
coimbra_location = [20, -8]
Person.geo_near(coimbra_location).spherical
Full text search
class Document
include Mongoid::Document
field :content, :type => String
index(
{:content => 'text'},
{:default_language => 'portuguese’}
)
end
Document.text_search('IKEA')
Caveats (not so nice)
No joins
Impact of key length
No integrity checks for references
Document size limit of 16MB
Nesting depth limit of 100
( Bundlr ) Examples
Settings
class User
include Mongoid::Document
embeds_one :settings
end
class Settings
include Mongoid:Document
field :receive_emails, type: Boolean
field :theme, type: Symbol
embedded_in :user
end
Oauths
class User
include Mongoid::Document
embeds_many :oauths
index [["oauths.provider", Mongo::ASCENDING],
["oauths.uid", Mongo::ASCENDING]]
end
class Oauth
include Mongoid:Document
field :provider, type: String
field :token, type: String
field :secret, type: String
field :uid, type: String
embedded_in :user
end
Activity Stream
class Activity
include Mongoid::Document
include Mongoid::Timestamps
field :verb, :type => Symbol
field :actor, :type => Hash
field :object, :type => Hash
field :target, :type => Hash
field :receivers, :type => Array
embeds_many :likes
end
Relationships
class User
include Mongoid::Document
has_and_belongs_to_many :followers, :class_name => "User",
:inverse_of => :followed_users
end
{
_id: ObjectId("4d541f6823380f670d000008"),
follower_ids: [
ObjectId("4d4ea8ec86d56d2907000003"),
ObjectId("50fe17c226c1b7000200018a")
],
}
Need smarter relationships? Embed them!
Other examples
Model hierarchy
Migrations
Changing schema?
No right way of doing things… is all about context!
Wrapping up…
Sérgio Santos
@sdsantos
me@sergiosantos.info
Simple MongoDB design for
Web (Rails) apps
Improve Coimbra

More Related Content

What's hot

The Ruby/mongoDB ecosystem
The Ruby/mongoDB ecosystemThe Ruby/mongoDB ecosystem
The Ruby/mongoDB ecosystem
Harold Giménez
 
Knockout vs. angular
Knockout vs. angularKnockout vs. angular
Knockout vs. angular
MaslowB
 
Downloading tor and getting to the silk road
Downloading tor and getting to the silk roadDownloading tor and getting to the silk road
Downloading tor and getting to the silk road
Cody Nos
 

What's hot (14)

Geolocalização com MongoDB e Rails
Geolocalização com MongoDB e RailsGeolocalização com MongoDB e Rails
Geolocalização com MongoDB e Rails
 
Schema Design by Example ~ MongoSF 2012
Schema Design by Example ~ MongoSF 2012Schema Design by Example ~ MongoSF 2012
Schema Design by Example ~ MongoSF 2012
 
Back to Basics Webinar 1 - Introduction to NoSQL
Back to Basics Webinar 1 - Introduction to NoSQLBack to Basics Webinar 1 - Introduction to NoSQL
Back to Basics Webinar 1 - Introduction to NoSQL
 
Back to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB ApplicationBack to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB Application
 
Back to Basics Webinar 3 - Thinking in Documents
Back to Basics Webinar 3 - Thinking in DocumentsBack to Basics Webinar 3 - Thinking in Documents
Back to Basics Webinar 3 - Thinking in Documents
 
MongoDB & Drupal
MongoDB & DrupalMongoDB & Drupal
MongoDB & Drupal
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
The Ruby/mongoDB ecosystem
The Ruby/mongoDB ecosystemThe Ruby/mongoDB ecosystem
The Ruby/mongoDB ecosystem
 
MongoDB dla administratora
MongoDB dla administratora MongoDB dla administratora
MongoDB dla administratora
 
Working with the Web: 
Decoding JSON
Working with the Web: 
Decoding JSONWorking with the Web: 
Decoding JSON
Working with the Web: 
Decoding JSON
 
Knockout vs. angular
Knockout vs. angularKnockout vs. angular
Knockout vs. angular
 
Mongo db 101 dc group
Mongo db 101 dc groupMongo db 101 dc group
Mongo db 101 dc group
 
Downloading tor and getting to the silk road
Downloading tor and getting to the silk roadDownloading tor and getting to the silk road
Downloading tor and getting to the silk road
 
Mongo db dla administratora
Mongo db dla administratoraMongo db dla administratora
Mongo db dla administratora
 

Viewers also liked (7)

Application Design per MongoDB
Application Design per MongoDBApplication Design per MongoDB
Application Design per MongoDB
 
introtomongodb
introtomongodbintrotomongodb
introtomongodb
 
The Right (and Wrong) Use Cases for MongoDB
The Right (and Wrong) Use Cases for MongoDBThe Right (and Wrong) Use Cases for MongoDB
The Right (and Wrong) Use Cases for MongoDB
 
Modeling Data in MongoDB
Modeling Data in MongoDBModeling Data in MongoDB
Modeling Data in MongoDB
 
Common MongoDB Use Cases
Common MongoDB Use Cases Common MongoDB Use Cases
Common MongoDB Use Cases
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema Design
 
MongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World ExamplesMongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World Examples
 

Similar to Simple MongoDB design for Rails apps

Webinar: Build an Application Series - Session 2 - Getting Started
Webinar: Build an Application Series - Session 2 - Getting StartedWebinar: Build an Application Series - Session 2 - Getting Started
Webinar: Build an Application Series - Session 2 - Getting Started
MongoDB
 
MongoDB and Ruby on Rails
MongoDB and Ruby on RailsMongoDB and Ruby on Rails
MongoDB and Ruby on Rails
rfischer20
 
Building Your First Application with MongoDB
Building Your First Application with MongoDBBuilding Your First Application with MongoDB
Building Your First Application with MongoDB
MongoDB
 
Mongoid in the real world
Mongoid in the real worldMongoid in the real world
Mongoid in the real world
Kevin Faustino
 

Similar to Simple MongoDB design for Rails apps (20)

Back to Basics 2017: Mí primera aplicación MongoDB
Back to Basics 2017: Mí primera aplicación MongoDBBack to Basics 2017: Mí primera aplicación MongoDB
Back to Basics 2017: Mí primera aplicación MongoDB
 
Mongo db basics
Mongo db basicsMongo db basics
Mongo db basics
 
Mongodb Introduction
Mongodb Introduction Mongodb Introduction
Mongodb Introduction
 
Mongo learning series
Mongo learning series Mongo learning series
Mongo learning series
 
Mongodb mongoid
Mongodb mongoidMongodb mongoid
Mongodb mongoid
 
MongoDB & Mongoid with Rails
MongoDB & Mongoid with RailsMongoDB & Mongoid with Rails
MongoDB & Mongoid with Rails
 
MongoDB
MongoDBMongoDB
MongoDB
 
Building your first app with MongoDB
Building your first app with MongoDBBuilding your first app with MongoDB
Building your first app with MongoDB
 
Webinar: Build an Application Series - Session 2 - Getting Started
Webinar: Build an Application Series - Session 2 - Getting StartedWebinar: Build an Application Series - Session 2 - Getting Started
Webinar: Build an Application Series - Session 2 - Getting Started
 
Webinar: Building Your First App
Webinar: Building Your First AppWebinar: Building Your First App
Webinar: Building Your First App
 
Mongodb With Mongoid
Mongodb With MongoidMongodb With Mongoid
Mongodb With Mongoid
 
Using Mongoid with Ruby on Rails
Using Mongoid with Ruby on RailsUsing Mongoid with Ruby on Rails
Using Mongoid with Ruby on Rails
 
MongoMapper lightning talk
MongoMapper lightning talkMongoMapper lightning talk
MongoMapper lightning talk
 
MongoDB and Ruby on Rails
MongoDB and Ruby on RailsMongoDB and Ruby on Rails
MongoDB and Ruby on Rails
 
Building Your First Application with MongoDB
Building Your First Application with MongoDBBuilding Your First Application with MongoDB
Building Your First Application with MongoDB
 
Building your first app with mongo db
Building your first app with mongo dbBuilding your first app with mongo db
Building your first app with mongo db
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
Analytics with MongoDB Aggregation Framework and Hadoop Connector
Analytics with MongoDB Aggregation Framework and Hadoop ConnectorAnalytics with MongoDB Aggregation Framework and Hadoop Connector
Analytics with MongoDB Aggregation Framework and Hadoop Connector
 
Webinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev TeamsWebinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev Teams
 
Mongoid in the real world
Mongoid in the real worldMongoid in the real world
Mongoid in the real world
 

More from Sérgio Santos (8)

Launching tech products
Launching tech productsLaunching tech products
Launching tech products
 
Ultra fast web development with sinatra
Ultra fast web development with sinatraUltra fast web development with sinatra
Ultra fast web development with sinatra
 
Agoge - produtividade & multitasking
Agoge - produtividade & multitaskingAgoge - produtividade & multitasking
Agoge - produtividade & multitasking
 
Ontologias
OntologiasOntologias
Ontologias
 
Workshop Django
Workshop DjangoWorkshop Django
Workshop Django
 
Gestão De Projectos
Gestão De ProjectosGestão De Projectos
Gestão De Projectos
 
Django Vs Rails
Django Vs RailsDjango Vs Rails
Django Vs Rails
 
Gestor - Casos De Uso
Gestor - Casos De UsoGestor - Casos De Uso
Gestor - Casos De Uso
 

Recently uploaded

Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 

Recently uploaded (20)

From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
Introduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationIntroduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG Evaluation
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 

Simple MongoDB design for Rails apps

Editor's Notes

  1. good - awesome for prototyping - fast development - freedom - fast growing community bad: size of parameters more responsability not many tools no native migrations, but there are alternatives - not nearly as used as PostgreSQL or MySQL - decisions decisions