SlideShare a Scribd company logo
with Ruby and MongoMapper
What's the presentation about?
To provide a basic introduction to MongoDB
An introduction to the MongoMapper Ruby
gem and how easy, readable the code is for
persisting/querying MongoDB
Caveat - “I’ve spent a few weeks learning about
this with Ruby and MongoMapper”
What the heck is MongoDB?
JSON (actually BSON - binary) based
datastore with 16MB document limit
Written in C++
Distros on Linux (RPM), OSX (Brew)
Lots of different language drivers available
(Ruby, R, Erlang, Python)
Why would I use it?
Simplicity - Building Javascript applications
without the layers of translation between
presentation -> business logic -> data access
Scaling out - rapidly without the hassle of
sharded MySQL or Postgres with Slony.
100GB+ on a single instance.
“It's quick to develop against - this is what I
want to use it for, prototyping!”
What’s Similar
CouchDB - although your datastore has to be
hardcoded up front in terms of views.
● It’s hard to do query filtering etc...
● Have to query through MapReduce JS
Who Uses MongoDB?
● Craigslist (Everybody uses it in North
America instead of Ebay)
● SAP - SaaS based platform
● MTV
● Sourceforge - backend storage
● Firebase (Datastore as a Service)
https://www.firebase.com
Mental Mapping
Table = Collection
Row = JSON Document
Index = Index
Join = Embedded Document
Partition = Shard
Partition Key = Shard Key
Useful Comparison Document
http://docs.mongodb.org/manual/reference/sql-comparison/
There's lots of API's including a native
MongoDB REST API, but...
I'm going to use:
● Ruby 1.9
● MongoMapper Gem
● ruby-mongo-driver (well the gem is)
Simple Storage Example
Install the gems for MongoMapper
gem install mongo_mapper
gem install bson_ext
or put the following in your Gemfile and use
bundler (http://bundler.io/):
source "https://rubygems.org"
gem "mongo_mapper"
gem "mongomapper_search"
Pre-requisites
Use mongod to start the database:
mongod --rest --dbpath=/home/jholloway/mongodb
Starting MongoDB
Native Port
Used by the MongoDB driver - in my case the
MongoMapper gem interacts with this
http://127.0.0.1:27017
Web UI
Allows us to inspect the MongoDB
http://127.0.0.1:28017
There's also a Mongo interactive shell (mongo)
MongoDB Interfaces
● I've got a number of ebay items I want to
store, specifically some retro computer
games I’m tracking prices of on Ebay
Overview
MongoMapper: Create a Document
Specify the ebay item definition using the
MongoMapper API
class Item
include MongoMapper::Document
key :name, String, :required => true
key :location, String
key :price, Float
end
MongoMapper: Save Document
Create a new item (n.b. use of Ruby symbols)
and call save on it
Item.new(:name => "Rescue from Fractalus",
:platform => "Commodore 64",
:location => "Chippenham",
:price => 2.50).save()
● Let's go back to the Mongo shell
./mongo
show dbs
use myebayitems
show collections
db.items.find()
Mongo Shell: Raw Storage
MongoMapper: Querying Documents
Will use irb for the examples here
irb -r ebayitemsave.rb
Find all items in the collection Item:
items = Item.all()
Query all items by price > 5
items = Item.all( :price => {:$gt => 5} )
MongoMapper: Dynamic Finders
I can also use dynamic finders using the fields
defined on my document, awesomeness.
So in the previous example find by price...
Item.find_by_price(2.50)
puts "Item: #{item.to_mongo()}"
MongoMapper: Embedded Docs
Joins - deeply nested JSON documents can be
a performance issue though
Need to think about database design carefully
up-front and model it as you’d expect to query it
This is very different from a relational database
with a reporting backend
http://mongomapper.com/documentation/
MongoMapper: MapReduce Example
MongoDB provides an aggregation framework
for simple operations
It also provides a mapReduce() command
which you can pass Javascript (yes) to the
embedded V8 engine
n.b. Dispatches the command to each shard in
a sharded MongoDB setup
MongoMapper: Full Text Search
Bah !
You can do it!
MongoMapper Search Gem - didn’t work for me
https://github.com/mariopeixoto/mongomapper_search
MongoMapper: Plugins
Fair few of them and you can write your own
easily enough:
● Associations
● Accessible
● Callbacks
● Dirty
● Keys
● Modifiers
● Protected
● Scopes
● Serialization
● Single Collection Inheritance
● Timestamps
● Userstamps
● Validations
MongoMapper: In Retrospect
I’ve just broken the surface with the capabilities
of it in the past few weeks and here
Lots more investigation required into the
aggregation framework and the map reduce
functionality
But it’s much much better than CouchDB for
what I wanted to do
Questions
Thanks!
All the code will be up here in a Github project:
https://github.com/jph98/ebaymongo
By the way we’re hiring...
● Javascript/Java Developers
● Devops
● R Consultants
Email: careers@mango-solutions.com

More Related Content

PDF
MongoDB Part 2
PDF
Slide Test
ODP
MongoDB and Node.js
PPTX
Java performance
PDF
Invitation to mongo db @ Rakuten TechTalk
PDF
How do i Meet MongoDB
PDF
PyConIT6 - MAKING SESSIONS AND CACHING ROOMMATES
PDF
PyConIT6 - Messing up with pymongo for fun and profit
MongoDB Part 2
Slide Test
MongoDB and Node.js
Java performance
Invitation to mongo db @ Rakuten TechTalk
How do i Meet MongoDB
PyConIT6 - MAKING SESSIONS AND CACHING ROOMMATES
PyConIT6 - Messing up with pymongo for fun and profit

What's hot (7)

PDF
appborg, coffeesurgeon, moof, logging-system
PDF
Node.js for Rubists
PDF
PDF
Pavel Prischepa.node load without-restrictions.DrupalCamp Kiev 2011
PPTX
A slightly advanced introduction to node.js
PPTX
Mongo db intro new
PPT
Brubeck: The Lightning Talk
appborg, coffeesurgeon, moof, logging-system
Node.js for Rubists
Pavel Prischepa.node load without-restrictions.DrupalCamp Kiev 2011
A slightly advanced introduction to node.js
Mongo db intro new
Brubeck: The Lightning Talk
Ad

Viewers also liked (8)

PPT
Cloud Computing And You
PDF
Artikel Leon Harinck (TriFinance) ControllersMagazine januari 2013: 'Nieuwe w...
PPT
Open Source & Open Cloud: Why License Is Important
PDF
Social Business Case Study - Enterasys Networks
PDF
Research Report: Cloud Trends in 2011 and beyond
PDF
MongoDB Mojo: Building a Basic Perl App
PPSX
The Magnet Journey
PPTX
Building The Pillars Of Modern Enterprise
Cloud Computing And You
Artikel Leon Harinck (TriFinance) ControllersMagazine januari 2013: 'Nieuwe w...
Open Source & Open Cloud: Why License Is Important
Social Business Case Study - Enterasys Networks
Research Report: Cloud Trends in 2011 and beyond
MongoDB Mojo: Building a Basic Perl App
The Magnet Journey
Building The Pillars Of Modern Enterprise
Ad

Similar to Introduction to using MongoDB with Ruby (20)

PDF
Mongodb
PDF
Java Persistence Frameworks for MongoDB
PPTX
BedCon 2013 - Java Persistenz-Frameworks für MongoDB
KEY
Rails with mongodb
PDF
Mongo db first steps with csharp
PDF
The elephant in the room mongo db + hadoop
KEY
Getting Started with MongoDB and Node.js
PPTX
MongoDB Introduction, Installation & Execution
PDF
Mongodb (1)
PDF
Mongo db bangalore
PDF
MongoDB and Node.js
PDF
Node Js, AngularJs and Express Js Tutorial
PPT
Rapid, Scalable Web Development with MongoDB, Ming, and Python
PPSX
Mongodb
PDF
MongoDB Tokyo - Monitoring and Queueing
PDF
Mongo db transcript
PPTX
Basics of MongoDB
PPTX
Analytical data processing
PPTX
Java Persistence Frameworks for MongoDB
PPTX
Webinar: Building Your First App in Node.js
Mongodb
Java Persistence Frameworks for MongoDB
BedCon 2013 - Java Persistenz-Frameworks für MongoDB
Rails with mongodb
Mongo db first steps with csharp
The elephant in the room mongo db + hadoop
Getting Started with MongoDB and Node.js
MongoDB Introduction, Installation & Execution
Mongodb (1)
Mongo db bangalore
MongoDB and Node.js
Node Js, AngularJs and Express Js Tutorial
Rapid, Scalable Web Development with MongoDB, Ming, and Python
Mongodb
MongoDB Tokyo - Monitoring and Queueing
Mongo db transcript
Basics of MongoDB
Analytical data processing
Java Persistence Frameworks for MongoDB
Webinar: Building Your First App in Node.js

More from Jonathan Holloway (11)

PPTX
The Role of the Architect
PPTX
Spring boot - an introduction
PPTX
Jenkins CI presentation
PPTX
Mockito intro
PPTX
PPTX
SOLID principles
PPTX
Application design for the cloud using AWS
PPTX
Building data pipelines
PPTX
Introduction to JVM languages and Fantom (very brief)
PDF
Database migration with flyway
PDF
Lightweight web frameworks
The Role of the Architect
Spring boot - an introduction
Jenkins CI presentation
Mockito intro
SOLID principles
Application design for the cloud using AWS
Building data pipelines
Introduction to JVM languages and Fantom (very brief)
Database migration with flyway
Lightweight web frameworks

Recently uploaded (20)

PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Big Data Technologies - Introduction.pptx
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Machine learning based COVID-19 study performance prediction
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
Cloud computing and distributed systems.
PDF
KodekX | Application Modernization Development
PDF
Modernizing your data center with Dell and AMD
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
A Presentation on Artificial Intelligence
Understanding_Digital_Forensics_Presentation.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
The AUB Centre for AI in Media Proposal.docx
Network Security Unit 5.pdf for BCA BBA.
Big Data Technologies - Introduction.pptx
NewMind AI Monthly Chronicles - July 2025
Diabetes mellitus diagnosis method based random forest with bat algorithm
NewMind AI Weekly Chronicles - August'25 Week I
MYSQL Presentation for SQL database connectivity
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
The Rise and Fall of 3GPP – Time for a Sabbatical?
Machine learning based COVID-19 study performance prediction
Encapsulation_ Review paper, used for researhc scholars
Chapter 3 Spatial Domain Image Processing.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Cloud computing and distributed systems.
KodekX | Application Modernization Development
Modernizing your data center with Dell and AMD
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
A Presentation on Artificial Intelligence

Introduction to using MongoDB with Ruby

  • 1. with Ruby and MongoMapper
  • 2. What's the presentation about? To provide a basic introduction to MongoDB An introduction to the MongoMapper Ruby gem and how easy, readable the code is for persisting/querying MongoDB Caveat - “I’ve spent a few weeks learning about this with Ruby and MongoMapper”
  • 3. What the heck is MongoDB? JSON (actually BSON - binary) based datastore with 16MB document limit Written in C++ Distros on Linux (RPM), OSX (Brew) Lots of different language drivers available (Ruby, R, Erlang, Python)
  • 4. Why would I use it? Simplicity - Building Javascript applications without the layers of translation between presentation -> business logic -> data access Scaling out - rapidly without the hassle of sharded MySQL or Postgres with Slony. 100GB+ on a single instance. “It's quick to develop against - this is what I want to use it for, prototyping!”
  • 5. What’s Similar CouchDB - although your datastore has to be hardcoded up front in terms of views. ● It’s hard to do query filtering etc... ● Have to query through MapReduce JS
  • 6. Who Uses MongoDB? ● Craigslist (Everybody uses it in North America instead of Ebay) ● SAP - SaaS based platform ● MTV ● Sourceforge - backend storage ● Firebase (Datastore as a Service) https://www.firebase.com
  • 7. Mental Mapping Table = Collection Row = JSON Document Index = Index Join = Embedded Document Partition = Shard Partition Key = Shard Key Useful Comparison Document http://docs.mongodb.org/manual/reference/sql-comparison/
  • 8. There's lots of API's including a native MongoDB REST API, but... I'm going to use: ● Ruby 1.9 ● MongoMapper Gem ● ruby-mongo-driver (well the gem is) Simple Storage Example
  • 9. Install the gems for MongoMapper gem install mongo_mapper gem install bson_ext or put the following in your Gemfile and use bundler (http://bundler.io/): source "https://rubygems.org" gem "mongo_mapper" gem "mongomapper_search" Pre-requisites
  • 10. Use mongod to start the database: mongod --rest --dbpath=/home/jholloway/mongodb Starting MongoDB
  • 11. Native Port Used by the MongoDB driver - in my case the MongoMapper gem interacts with this http://127.0.0.1:27017 Web UI Allows us to inspect the MongoDB http://127.0.0.1:28017 There's also a Mongo interactive shell (mongo) MongoDB Interfaces
  • 12. ● I've got a number of ebay items I want to store, specifically some retro computer games I’m tracking prices of on Ebay Overview
  • 13. MongoMapper: Create a Document Specify the ebay item definition using the MongoMapper API class Item include MongoMapper::Document key :name, String, :required => true key :location, String key :price, Float end
  • 14. MongoMapper: Save Document Create a new item (n.b. use of Ruby symbols) and call save on it Item.new(:name => "Rescue from Fractalus", :platform => "Commodore 64", :location => "Chippenham", :price => 2.50).save()
  • 15. ● Let's go back to the Mongo shell ./mongo show dbs use myebayitems show collections db.items.find() Mongo Shell: Raw Storage
  • 16. MongoMapper: Querying Documents Will use irb for the examples here irb -r ebayitemsave.rb Find all items in the collection Item: items = Item.all() Query all items by price > 5 items = Item.all( :price => {:$gt => 5} )
  • 17. MongoMapper: Dynamic Finders I can also use dynamic finders using the fields defined on my document, awesomeness. So in the previous example find by price... Item.find_by_price(2.50) puts "Item: #{item.to_mongo()}"
  • 18. MongoMapper: Embedded Docs Joins - deeply nested JSON documents can be a performance issue though Need to think about database design carefully up-front and model it as you’d expect to query it This is very different from a relational database with a reporting backend http://mongomapper.com/documentation/
  • 19. MongoMapper: MapReduce Example MongoDB provides an aggregation framework for simple operations It also provides a mapReduce() command which you can pass Javascript (yes) to the embedded V8 engine n.b. Dispatches the command to each shard in a sharded MongoDB setup
  • 20. MongoMapper: Full Text Search Bah ! You can do it! MongoMapper Search Gem - didn’t work for me https://github.com/mariopeixoto/mongomapper_search
  • 21. MongoMapper: Plugins Fair few of them and you can write your own easily enough: ● Associations ● Accessible ● Callbacks ● Dirty ● Keys ● Modifiers ● Protected ● Scopes ● Serialization ● Single Collection Inheritance ● Timestamps ● Userstamps ● Validations
  • 22. MongoMapper: In Retrospect I’ve just broken the surface with the capabilities of it in the past few weeks and here Lots more investigation required into the aggregation framework and the map reduce functionality But it’s much much better than CouchDB for what I wanted to do
  • 23. Questions Thanks! All the code will be up here in a Github project: https://github.com/jph98/ebaymongo
  • 24. By the way we’re hiring... ● Javascript/Java Developers ● Devops ● R Consultants Email: careers@mango-solutions.com