SlideShare una empresa de Scribd logo
1 de 38
Descargar para leer sin conexión
Webinar: Getting Started
with MongoDB and Ruby
Brandon Black
Software Engineer, MongoDB
@brandonmblack
+
“I believe people want to express themselves when they
program. They don't want to fight with the language.
Programming languages must feel natural to
programmers. I tried to make people enjoy programming
and concentrate on the fun and creative part of
programming when they use Ruby.”

— Yukihiro “Matz” Matsumoto, Creator of Ruby (2001)
What is MongoDB?
MongoDB is a ___________ database
•
•
•
•
•

Document
Open source
High performance
Horizontally scalable
Full featured
Document Database
•
•
•
•
•
•

Not for .PDF & .DOC files
A document is essentially an associative array
Document = JSON object
Document = PHP Array
Document = Python Dict
Document = Ruby Hash
Open-Source
•
•
•
•
•
•

MongoDB is an open source project
Available on GitHub
Licensed under the AGPL
Started & sponsored by MongoDB, Inc. (10gen)
Commercial licenses available
Contributions welcome
High Performance
• Written in C++
• Extensive use of memory-mapped files 

i.e. read-through write-through memory caching.
• Runs nearly everywhere
• Data serialized as BSON (fast parsing)
• Full support for primary & secondary indexes
• Document model = less work
Shard 1

Shard 2

Shard 3

Horizontally Scalable

Shard N
Scalability & Performance

Data Landscape
Memcached
MongoDB

RDBMS

Depth of Functionality
Full Featured
•
•
•
•
•
•
•

Ad Hoc queries
Real time aggregation
Rich query capabilities
Strongly consistent
Geospatial features
Native support for most programming languages
Flexible schema (not schema-less!)
Installation & Setup
1 Download
From website:
http://www.mongodb.org/downloads
Package manager:
sudo apt-get install mongodb-10gen
brew install mongodb

2 Startup MongoDB
mongod —dbpath /path/to/data

3 Connect with the mongo Shell
Or in our case… IRB/Pry!
Using MongoDB with
Ruby
Using the Ruby Driver
Client Application
Driver

Re
a

ad
Re

d

Write

Secondary

Primary

Secondary
Using the Ruby Driver
# install gem and native extensions (optional)!
gem install mongo
gem install bson_ext

require 'mongo'!
include Mongo!

!
# connecting to the database!
client = MongoClient.new # defaults to localhost:27017!
client = MongoClient.new('host1.example.com')!
client = MongoClient.new('host1.example.com', 27017)!
# using configuration options!
opts
= { :pool_size => 5, :pool_timeout => 5 }!
client = MongoClient.new('host1.example.com', 27017, opts)!
Using the Ruby Driver
seeds = ['h1.example.com:27017',!
'h2.example.com:27017',!
'h3.example.com:27017']!

!
# connecting to a replica set!
client = MongoReplicaSetClient.new(seeds)!
client = MongoReplicaSetClient.new(seeds, :read => :secondary)!
# connecting to a sharded cluster!
client = MongoShardedClient.new(seeds)!
client = MongoShardedClient.new(seeds, :read => :secondary)!
# using a connection string!
ENV['MONGODB_URI'] = 'mongodb://host1:27017?ssl=true'!
client = MongoClient.new!
Using the Ruby Driver
# using a database!
db = client.db('blog')!
db = client['blog']!

!
client.drop_database('blog')!
client.database_names!

!
# using a collection!
coll = db['posts']!

!
coll.drop!
db.collection_names!
Terminology
RDBMS

MongoDB

Table, View

Collection

Row

Document

Index

Index

Join

Embedded Document

Foreign Key

Reference

Partition

Shard
Building a Blog
Models/Entities:
•

Users (Authors)

•

Articles

•

Comments

•

Tags/Categories
In a relational application
we would start by defining
our schema.
Typical Relational Diagram
Category
·Name
·URL

User
·Name
·Email address

Article
·Name
·Slug
·Publish date
·Text

Comment
·Comment
·Date
·Author

Tag
·Name
·URL
In MongoDB we start building
and we let the schema evolve
as we go.
Like Ruby, it has a natural and
enjoyable feeling to it!
MongoDB Diagram
Article

User
·Name
·Email address

·Name
·Slug
·Publish date
·Text
·Author

Comment[]
·Comment
·Date
·Author

Tag[]
·Value

Category[]
·Value
Inserting a New Document
# example document!
author = {!
:username => 'brandonblack',!
:first
=> 'brandon',!
:last
=> 'black'!
}!

!
# inserting into my blog database!
client['blog']['users'].insert(author)

No database or collection
creation required!
Inserting a New Document
# example document!
article = {!
:title
=> 'Hello World'!
:username => 'brandonblack',!
:tags
=> ['ruby', 'getting started'],!
:comments => [ # embedded docs ]!
}!

!
!
# inserting into my blog database!
client['blog']['articles'].insert(article)

No database or collection
creation required!
Finding Documents
coll = client['blog']['users']!

!
# finding a single document!
coll.find_one!
coll.find_one({ :username => 'brandonblack' })!
coll.find_one({ :username => 'brandonblack' }, { :first => 1})!

!
coll = client['blog']['articles']!

!
# finding multiple documents (using cursors)!
cursor = coll.find({ :username => 'brandonblack' }, :limit => 10)!
cursor.each do |article|!
puts article['title']!
end!
Updating and Deleting Documents
# updating a document!
article = { :title => 'Hello Ruby' }!
coll.update({ '_id' => article_id }, article)!
coll.update({ '_id' => article_id },!
{'$set' => {:title => 'Hello Ruby'}})!

!
# deleting a single document!
coll.remove({ '_id' => article_id })!

!
# delete multiple documents!
coll.remove({ 'username' => 'brandonblack' })!

!
# delete all documents in a collection!
coll.remove!
Indexes
# running explain on a query!
coll.find({ :username => 'brandonblack' }).explain!

!

# showing collection indexes!
coll.index_information!

!

# adding an index!
coll.ensure_index({ :username => 1 })!
coll.ensure_index({ :username => Mongo::ASCENDING })!

!

coll.ensure_index({ :username => -1 })!
coll.ensure_index({ :username => Mongo::DESCENDING })!

!

# adding a special index types!
coll.ensure_index({ :loc => Mongo::GEO2D })!
coll.ensure_index({ :title => Mongo::TEXT })!

!

# droping an index!
coll.drop_index('username_1')!

!

# dropping all indexes for a collection!
coll.drop_indexes!
ODMs (Object Document Mappers)
Mongoid (http://mongoid.org/)
Mongo Mapper (http://mongomapper.com/)
Mongoid Example
# rails setup!
rails g mongoid:config!

!
# non-rails setup!
Mongoid.load!("path/to/your/mongoid.yml", :production)!

!
# document examples!
class Article!
include Mongoid::Document!
field :title, type: String!
embeds_many :comments!
end!

!
class Comment!
include Mongoid::Document!
field :comment_text, type: String!
embedded_in :article!
end!
What Next?
We’ve covered a lot of
ground quickly.
Come Find Us!
•

Github

•

Stack Overflow

•

MongoDB User Group
Further Reading
More Ways to Learn
Free Online Courses
http://education.mongodb.com/

Events & Webinars
http://www.mongodb.com/events

Presentations
http://www.mongodb.com/presentations
Thank you!
Brandon Black
Software Engineer, MongoDB
@brandonmblack

Más contenido relacionado

La actualidad más candente

Build web application by express
Build web application by expressBuild web application by express
Build web application by express
Shawn Meng
 
PHP Web Development
PHP Web DevelopmentPHP Web Development
PHP Web Development
gaplabs
 
Using shortcode in plugin development
Using shortcode in plugin developmentUsing shortcode in plugin development
Using shortcode in plugin development
gskhanal
 

La actualidad más candente (18)

Build web application by express
Build web application by expressBuild web application by express
Build web application by express
 
JSON REST API for WordPress
JSON REST API for WordPressJSON REST API for WordPress
JSON REST API for WordPress
 
A slightly advanced introduction to node.js
A slightly advanced introduction to node.jsA slightly advanced introduction to node.js
A slightly advanced introduction to node.js
 
Presentation of JSConf.eu
Presentation of JSConf.euPresentation of JSConf.eu
Presentation of JSConf.eu
 
Djangocon 2014 angular + django
Djangocon 2014 angular + djangoDjangocon 2014 angular + django
Djangocon 2014 angular + django
 
The JSON REST API for WordPress
The JSON REST API for WordPressThe JSON REST API for WordPress
The JSON REST API for WordPress
 
PHP Web Development
PHP Web DevelopmentPHP Web Development
PHP Web Development
 
Server side scripting smack down - Node.js vs PHP
Server side scripting smack down - Node.js vs PHPServer side scripting smack down - Node.js vs PHP
Server side scripting smack down - Node.js vs PHP
 
4Developers: Michał Papis- Publikowanie gemów
4Developers: Michał Papis- Publikowanie gemów4Developers: Michał Papis- Publikowanie gemów
4Developers: Michał Papis- Publikowanie gemów
 
moma-django overview --> Django + MongoDB: building a custom ORM layer
moma-django overview --> Django + MongoDB: building a custom ORM layermoma-django overview --> Django + MongoDB: building a custom ORM layer
moma-django overview --> Django + MongoDB: building a custom ORM layer
 
AMD - Why, What and How
AMD - Why, What and HowAMD - Why, What and How
AMD - Why, What and How
 
Node.js Express
Node.js  ExpressNode.js  Express
Node.js Express
 
Using shortcode in plugin development
Using shortcode in plugin developmentUsing shortcode in plugin development
Using shortcode in plugin development
 
Php converted pdf
Php converted pdfPhp converted pdf
Php converted pdf
 
Ppt php
Ppt phpPpt php
Ppt php
 
Updates to the java api for json processing for java ee 8
Updates to the java api for json processing for java ee 8Updates to the java api for json processing for java ee 8
Updates to the java api for json processing for java ee 8
 
Differential Sync and JSON Patch @ SpringOne2GX 2014
Differential Sync and JSON Patch @ SpringOne2GX 2014Differential Sync and JSON Patch @ SpringOne2GX 2014
Differential Sync and JSON Patch @ SpringOne2GX 2014
 
php (Hypertext Preprocessor)
php (Hypertext Preprocessor)php (Hypertext Preprocessor)
php (Hypertext Preprocessor)
 

Similar a Webinar: Getting Started with Ruby and MongoDB

Building Your First Application with MongoDB
Building Your First Application with MongoDBBuilding Your First Application with MongoDB
Building Your First Application with MongoDB
MongoDB
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDB
MongoDB
 
MongoDB and Ruby on Rails
MongoDB and Ruby on RailsMongoDB and Ruby on Rails
MongoDB and Ruby on Rails
rfischer20
 
Mongo db eveningschemadesign
Mongo db eveningschemadesignMongo db eveningschemadesign
Mongo db eveningschemadesign
MongoDB APAC
 
Parse cloud code
Parse cloud codeParse cloud code
Parse cloud code
維佋 唐
 

Similar a Webinar: Getting Started with Ruby and MongoDB (20)

Webinar: Building Your First MongoDB App
Webinar: Building Your First MongoDB AppWebinar: Building Your First MongoDB App
Webinar: Building Your First MongoDB App
 
Building your first app with MongoDB
Building your first app with MongoDBBuilding your first app with MongoDB
Building your first app with MongoDB
 
Building Your First Application with MongoDB
Building Your First Application with MongoDBBuilding Your First Application with MongoDB
Building Your First Application with MongoDB
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDB
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDB
 
MongoDB and Ruby on Rails
MongoDB and Ruby on RailsMongoDB and Ruby on Rails
MongoDB and Ruby on Rails
 
Webinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.jsWebinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.js
 
Webinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.jsWebinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.js
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDB
 
MongoDB and Node.js
MongoDB and Node.jsMongoDB and Node.js
MongoDB and Node.js
 
Mongo db eveningschemadesign
Mongo db eveningschemadesignMongo db eveningschemadesign
Mongo db eveningschemadesign
 
Webinar: Building Your First App
Webinar: Building Your First AppWebinar: Building Your First App
Webinar: Building Your First App
 
10gen MongoDB Video Presentation at WebGeek DevCup
10gen MongoDB Video Presentation at WebGeek DevCup10gen MongoDB Video Presentation at WebGeek DevCup
10gen MongoDB Video Presentation at WebGeek DevCup
 
MongoDB
MongoDBMongoDB
MongoDB
 
MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...
MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...
MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...
 
MongoDB World 2018: Tutorial - Got Dibs? Building a Real-Time Bidding App wit...
MongoDB World 2018: Tutorial - Got Dibs? Building a Real-Time Bidding App wit...MongoDB World 2018: Tutorial - Got Dibs? Building a Real-Time Bidding App wit...
MongoDB World 2018: Tutorial - Got Dibs? Building a Real-Time Bidding App wit...
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDB
 
Webinar: What's new in the .NET Driver
Webinar: What's new in the .NET DriverWebinar: What's new in the .NET Driver
Webinar: What's new in the .NET Driver
 
Dev Jumpstart: Building Your First App
Dev Jumpstart: Building Your First AppDev Jumpstart: Building Your First App
Dev Jumpstart: Building Your First App
 
Parse cloud code
Parse cloud codeParse cloud code
Parse cloud code
 

Más de MongoDB

Más de MongoDB (20)

MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB AtlasMongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
 
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
 
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
 
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDBMongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
 
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
 
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series DataMongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
 
MongoDB SoCal 2020: MongoDB Atlas Jump Start
 MongoDB SoCal 2020: MongoDB Atlas Jump Start MongoDB SoCal 2020: MongoDB Atlas Jump Start
MongoDB SoCal 2020: MongoDB Atlas Jump Start
 
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
 
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
 
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
 
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
 
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your MindsetMongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
 
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: MongoDB Atlas JumpstartMongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
 
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
 
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
 
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
 
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep DiveMongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
 
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & GolangMongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
 
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
 
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
 

Último

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Último (20)

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

Webinar: Getting Started with Ruby and MongoDB

  • 1. Webinar: Getting Started with MongoDB and Ruby Brandon Black Software Engineer, MongoDB @brandonmblack
  • 2.
  • 3.
  • 4. +
  • 5. “I believe people want to express themselves when they program. They don't want to fight with the language. Programming languages must feel natural to programmers. I tried to make people enjoy programming and concentrate on the fun and creative part of programming when they use Ruby.” — Yukihiro “Matz” Matsumoto, Creator of Ruby (2001)
  • 7. MongoDB is a ___________ database • • • • • Document Open source High performance Horizontally scalable Full featured
  • 8. Document Database • • • • • • Not for .PDF & .DOC files A document is essentially an associative array Document = JSON object Document = PHP Array Document = Python Dict Document = Ruby Hash
  • 9. Open-Source • • • • • • MongoDB is an open source project Available on GitHub Licensed under the AGPL Started & sponsored by MongoDB, Inc. (10gen) Commercial licenses available Contributions welcome
  • 10. High Performance • Written in C++ • Extensive use of memory-mapped files 
 i.e. read-through write-through memory caching. • Runs nearly everywhere • Data serialized as BSON (fast parsing) • Full support for primary & secondary indexes • Document model = less work
  • 11. Shard 1 Shard 2 Shard 3 Horizontally Scalable Shard N
  • 12. Scalability & Performance Data Landscape Memcached MongoDB RDBMS Depth of Functionality
  • 13. Full Featured • • • • • • • Ad Hoc queries Real time aggregation Rich query capabilities Strongly consistent Geospatial features Native support for most programming languages Flexible schema (not schema-less!)
  • 14. Installation & Setup 1 Download From website: http://www.mongodb.org/downloads Package manager: sudo apt-get install mongodb-10gen brew install mongodb 2 Startup MongoDB mongod —dbpath /path/to/data 3 Connect with the mongo Shell Or in our case… IRB/Pry!
  • 16. Using the Ruby Driver Client Application Driver Re a ad Re d Write Secondary Primary Secondary
  • 17. Using the Ruby Driver # install gem and native extensions (optional)! gem install mongo gem install bson_ext require 'mongo'! include Mongo! ! # connecting to the database! client = MongoClient.new # defaults to localhost:27017! client = MongoClient.new('host1.example.com')! client = MongoClient.new('host1.example.com', 27017)! # using configuration options! opts = { :pool_size => 5, :pool_timeout => 5 }! client = MongoClient.new('host1.example.com', 27017, opts)!
  • 18. Using the Ruby Driver seeds = ['h1.example.com:27017',! 'h2.example.com:27017',! 'h3.example.com:27017']! ! # connecting to a replica set! client = MongoReplicaSetClient.new(seeds)! client = MongoReplicaSetClient.new(seeds, :read => :secondary)! # connecting to a sharded cluster! client = MongoShardedClient.new(seeds)! client = MongoShardedClient.new(seeds, :read => :secondary)! # using a connection string! ENV['MONGODB_URI'] = 'mongodb://host1:27017?ssl=true'! client = MongoClient.new!
  • 19. Using the Ruby Driver # using a database! db = client.db('blog')! db = client['blog']! ! client.drop_database('blog')! client.database_names! ! # using a collection! coll = db['posts']! ! coll.drop! db.collection_names!
  • 21. Building a Blog Models/Entities: • Users (Authors) • Articles • Comments • Tags/Categories
  • 22. In a relational application we would start by defining our schema.
  • 23. Typical Relational Diagram Category ·Name ·URL User ·Name ·Email address Article ·Name ·Slug ·Publish date ·Text Comment ·Comment ·Date ·Author Tag ·Name ·URL
  • 24. In MongoDB we start building and we let the schema evolve as we go. Like Ruby, it has a natural and enjoyable feeling to it!
  • 25. MongoDB Diagram Article User ·Name ·Email address ·Name ·Slug ·Publish date ·Text ·Author Comment[] ·Comment ·Date ·Author Tag[] ·Value Category[] ·Value
  • 26. Inserting a New Document # example document! author = {! :username => 'brandonblack',! :first => 'brandon',! :last => 'black'! }! ! # inserting into my blog database! client['blog']['users'].insert(author) No database or collection creation required!
  • 27. Inserting a New Document # example document! article = {! :title => 'Hello World'! :username => 'brandonblack',! :tags => ['ruby', 'getting started'],! :comments => [ # embedded docs ]! }! ! ! # inserting into my blog database! client['blog']['articles'].insert(article) No database or collection creation required!
  • 28. Finding Documents coll = client['blog']['users']! ! # finding a single document! coll.find_one! coll.find_one({ :username => 'brandonblack' })! coll.find_one({ :username => 'brandonblack' }, { :first => 1})! ! coll = client['blog']['articles']! ! # finding multiple documents (using cursors)! cursor = coll.find({ :username => 'brandonblack' }, :limit => 10)! cursor.each do |article|! puts article['title']! end!
  • 29. Updating and Deleting Documents # updating a document! article = { :title => 'Hello Ruby' }! coll.update({ '_id' => article_id }, article)! coll.update({ '_id' => article_id },! {'$set' => {:title => 'Hello Ruby'}})! ! # deleting a single document! coll.remove({ '_id' => article_id })! ! # delete multiple documents! coll.remove({ 'username' => 'brandonblack' })! ! # delete all documents in a collection! coll.remove!
  • 30. Indexes # running explain on a query! coll.find({ :username => 'brandonblack' }).explain! ! # showing collection indexes! coll.index_information! ! # adding an index! coll.ensure_index({ :username => 1 })! coll.ensure_index({ :username => Mongo::ASCENDING })! ! coll.ensure_index({ :username => -1 })! coll.ensure_index({ :username => Mongo::DESCENDING })! ! # adding a special index types! coll.ensure_index({ :loc => Mongo::GEO2D })! coll.ensure_index({ :title => Mongo::TEXT })! ! # droping an index! coll.drop_index('username_1')! ! # dropping all indexes for a collection! coll.drop_indexes!
  • 31. ODMs (Object Document Mappers) Mongoid (http://mongoid.org/) Mongo Mapper (http://mongomapper.com/)
  • 32. Mongoid Example # rails setup! rails g mongoid:config! ! # non-rails setup! Mongoid.load!("path/to/your/mongoid.yml", :production)! ! # document examples! class Article! include Mongoid::Document! field :title, type: String! embeds_many :comments! end! ! class Comment! include Mongoid::Document! field :comment_text, type: String! embedded_in :article! end!
  • 34. We’ve covered a lot of ground quickly.
  • 35. Come Find Us! • Github • Stack Overflow • MongoDB User Group
  • 37. More Ways to Learn Free Online Courses http://education.mongodb.com/ Events & Webinars http://www.mongodb.com/events Presentations http://www.mongodb.com/presentations
  • 38. Thank you! Brandon Black Software Engineer, MongoDB @brandonmblack