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
Simple MongoDB design for Rails apps
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 (14)

KEY
Geolocalização com MongoDB e Rails
Maurício Maia
 
KEY
Schema Design by Example ~ MongoSF 2012
hungarianhc
 
PPTX
Back to Basics Webinar 1 - Introduction to NoSQL
Joe Drumgoole
 
PPTX
Back to Basics Webinar 2: Your First MongoDB Application
MongoDB
 
PPTX
Back to Basics Webinar 3 - Thinking in Documents
Joe Drumgoole
 
PPTX
MongoDB & Drupal
Паша Павел
 
PPTX
Introduction to MongoDB
Algiers Tech Meetup
 
KEY
The Ruby/mongoDB ecosystem
Harold Giménez
 
PDF
MongoDB dla administratora
3camp
 
PDF
Working with the Web: 
Decoding JSON
SV.CO
 
PDF
Knockout vs. angular
MaslowB
 
PPTX
Mongo db 101 dc group
John Ragan
 
DOCX
Downloading tor and getting to the silk road
Cody Nos
 
ODP
Mongo db dla administratora
Łukasz Jagiełło
 
Geolocalização com MongoDB e Rails
Maurício Maia
 
Schema Design by Example ~ MongoSF 2012
hungarianhc
 
Back to Basics Webinar 1 - Introduction to NoSQL
Joe Drumgoole
 
Back to Basics Webinar 2: Your First MongoDB Application
MongoDB
 
Back to Basics Webinar 3 - Thinking in Documents
Joe Drumgoole
 
MongoDB & Drupal
Паша Павел
 
Introduction to MongoDB
Algiers Tech Meetup
 
The Ruby/mongoDB ecosystem
Harold Giménez
 
MongoDB dla administratora
3camp
 
Working with the Web: 
Decoding JSON
SV.CO
 
Knockout vs. angular
MaslowB
 
Mongo db 101 dc group
John Ragan
 
Downloading tor and getting to the silk road
Cody Nos
 
Mongo db dla administratora
Łukasz Jagiełło
 

Viewers also liked (7)

PDF
Application Design per MongoDB
Codemotion
 
PPTX
introtomongodb
saikiran
 
PPTX
The Right (and Wrong) Use Cases for MongoDB
MongoDB
 
KEY
Modeling Data in MongoDB
lehresman
 
PPTX
Common MongoDB Use Cases
MongoDB
 
PPT
MongoDB Schema Design
MongoDB
 
PPTX
MongoDB Schema Design: Four Real-World Examples
Mike Friedman
 
Application Design per MongoDB
Codemotion
 
introtomongodb
saikiran
 
The Right (and Wrong) Use Cases for MongoDB
MongoDB
 
Modeling Data in MongoDB
lehresman
 
Common MongoDB Use Cases
MongoDB
 
MongoDB Schema Design
MongoDB
 
MongoDB Schema Design: Four Real-World Examples
Mike Friedman
 
Ad

Similar to Simple MongoDB design for Rails apps (20)

PDF
MongoDB and Ruby on Rails
rfischer20
 
PPTX
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
MongoDB
 
PDF
Mongoid in the real world
Kevin Faustino
 
PDF
Using Mongoid with Ruby on Rails
Nicholas Altobelli
 
PDF
Mongo db eveningschemadesign
MongoDB APAC
 
PPTX
Webinar: General Technical Overview of MongoDB for Dev Teams
MongoDB
 
PDF
Building your first app with MongoDB
Norberto Leite
 
PDF
Back to Basics 2017: Mí primera aplicación MongoDB
MongoDB
 
PPTX
Introduction to MongoDB
Hossein Boustani
 
PDF
Mongodb mongoid
Marcos Vanetta
 
KEY
Introduction to MongoDB
Alex Bilbie
 
PDF
OSDC 2012 | Building a first application on MongoDB by Ross Lawley
NETWAYS
 
PDF
Mongodb Introduction
Raghvendra Parashar
 
PDF
Using MongoDB and a Relational Database at MongoDB Day
hayesdavis
 
KEY
MongoDB, PHP and the cloud - php cloud summit 2011
Steven Francia
 
PPTX
Conceptos básicos. Seminario web 2: Su primera aplicación MongoDB
MongoDB
 
KEY
Schema Design with MongoDB
rogerbodamer
 
PPTX
Intro To Mongo Db
chriskite
 
PDF
MongoDB Schema Design Tips & Tricks
Juan Antonio Roy Couto
 
PDF
Building Your First MongoDB Application
Tugdual Grall
 
MongoDB and Ruby on Rails
rfischer20
 
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
MongoDB
 
Mongoid in the real world
Kevin Faustino
 
Using Mongoid with Ruby on Rails
Nicholas Altobelli
 
Mongo db eveningschemadesign
MongoDB APAC
 
Webinar: General Technical Overview of MongoDB for Dev Teams
MongoDB
 
Building your first app with MongoDB
Norberto Leite
 
Back to Basics 2017: Mí primera aplicación MongoDB
MongoDB
 
Introduction to MongoDB
Hossein Boustani
 
Mongodb mongoid
Marcos Vanetta
 
Introduction to MongoDB
Alex Bilbie
 
OSDC 2012 | Building a first application on MongoDB by Ross Lawley
NETWAYS
 
Mongodb Introduction
Raghvendra Parashar
 
Using MongoDB and a Relational Database at MongoDB Day
hayesdavis
 
MongoDB, PHP and the cloud - php cloud summit 2011
Steven Francia
 
Conceptos básicos. Seminario web 2: Su primera aplicación MongoDB
MongoDB
 
Schema Design with MongoDB
rogerbodamer
 
Intro To Mongo Db
chriskite
 
MongoDB Schema Design Tips & Tricks
Juan Antonio Roy Couto
 
Building Your First MongoDB Application
Tugdual Grall
 
Ad

More from Sérgio Santos (8)

PDF
Launching tech products
Sérgio Santos
 
PPTX
Ultra fast web development with sinatra
Sérgio Santos
 
PDF
Agoge - produtividade & multitasking
Sérgio Santos
 
PDF
Ontologias
Sérgio Santos
 
PDF
Workshop Django
Sérgio Santos
 
PPTX
Gestão De Projectos
Sérgio Santos
 
PDF
Django Vs Rails
Sérgio Santos
 
PPTX
Gestor - Casos De Uso
Sérgio Santos
 
Launching tech products
Sérgio Santos
 
Ultra fast web development with sinatra
Sérgio Santos
 
Agoge - produtividade & multitasking
Sérgio Santos
 
Ontologias
Sérgio Santos
 
Workshop Django
Sérgio Santos
 
Gestão De Projectos
Sérgio Santos
 
Django Vs Rails
Sérgio Santos
 
Gestor - Casos De Uso
Sérgio Santos
 

Recently uploaded (20)

PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 

Simple MongoDB design for Rails apps

Editor's Notes

  • #39: 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