SlideShare a Scribd company logo
MongoDB and NoSQL
  ...and all of its awesomeness....
What is MongoDB?
● MongoDB (from 'humongous') is a scalable,
  high-performance, open source NoSQL
  database.
● JSON-style document storage
  ○ actually BSON (Binary JSON)
● Awesome
Terminology
●   db > collection > document
●   collection = table
●   document = row
●   Database is made of of collections
●   Collections are made up of documents
Why Should I Use MongoDB (Pros)
● Flexible: database changes are easy
  because it's a dynamic schema.
● Fast: Indexes are stored in memory which
  alleviates need for additional cache
  dependency
● Easy to scale with replica sets, sharding
● Atomic updates: All fail or all succeed
 
Why to Not Use MongoDB (Cons)
● Non transactional
● Document Size Limitations 16MB
● Ambiguous keys when shortened
MongoDB Syntax
INSERT
  > db.foo.insert(doc_or_docs)
SELECT
  > db.foo.find(criteria)
UPDATE
  > db.foo.update(criteria, updates, upsert, multi)
DELETE
  > db.foo.remove(criteria)
Blog Post Example
● Create a Blog Post
● Retrieve a Blog Post
● Update a Blog Post
  ○ $inc, $set, $unset, $push, $pushAll, $addToSet and
    $each, $pop, $pull, $pullAll, $rename
● Delete a Blog Post
Define the Model
class BlogPost():   In SQL world, most of you already can
     id             see this would require 3 separate
     name           tables:
     content
                    1.   BlogPosts
     author_id      2.   Comments
                    3.   Tags
class Comments():
     author_id
     comment
     blog_id

class Tags():
     name
     blog_id
Mongo Document Storage Structure
Document structure inside of the "BlogPost" collection:
 
{
    'id': 12345,
    'name': 'Hello World',
    'author_id': 112233,
    'content': 'This is my blog post content. Woot woot!',
    'comments': [
          {'user_id': 4321, 'comment': 'Awesome blog!'},
          {'user_id': 1234, 'comment': 'Totally Agree'}
    ],
    'tags': ['hello', 'world']
}
Demo
Enough talking...show some freaking code...
Demo Code: Insert
                                                         doc2 = {'id': 67890,
doc = {'id': 12345,
                                                         'name': 'Another Hello World',
'name': 'Hello World',
                                                         'author_id': 112233,
'author_id': 112233,
                                                         'content': 'This is my second blog post',
'content': 'This is my blog post content. Woot woot!',
                                                         'comments': [
'comments': [
                                                           {'user_id': 4321, 'comment': 'Even better than
  {'user_id': 4321, 'comment': 'Awesome blog!'},
                                                         the first!'},
  {'user_id': 1234, 'comment': 'Totally Agree'}
                                                           {'user_id': 1234, 'comment': 'You rock'}
],
                                                         ],
'tags': ['hello', 'world']
                                                         'tags': ['jimmy', 'buffet']
}
                                                         }
 
                                                          
db.blogpost.insert(doc)
                                                         db.blogpost.insert(doc2)
 
 
Demo Code: Retrieve
Return the whole doc:
  ● db.blogpost.findOne()
  ● db.blogpost.findOne({'id': 12345})
 
Only return specific fields:
  ● db.blogpost.findOne({}, {'name':1, 'author_id':1})
 
Exclude specific fields:
  ● db.blogpost.findOne({}, {'comments': 0})
 
Find all blog posts by author 112233:
  ● db.blogpost.find({'id': 112233}).pretty()
Demo Code: Update
Update all posts by Author_id:
    ●   db.blogpost.update({'author_id': 112233},
                           {'$set':{'is_featured': true}}, false, true)
 
Update all posts by author_id using tag "buffet":
 ● db.blogpost.update({'author_id': 112233, 'tags': 'buffet'},
                              {'$set':{'rock_star': true}}, false, true)
 
Update all posts by author_id where comment == 'You rock':
    ●   db.blogpost.update({'author_id': 112233, 'comments.comment': 'You rock'},
                            {'$set':{'comments.$.about_rock': true}}, false, true)
                    
Add tag to all posts by author_id:
    ●   db.blogpost.update({'author_id': 112233},
                           {'$addToSet':{'tags': 'word up'}}, false, true)
Demo Code: Delete
Remove by document id:
  ● db.blogpost.remove({'id': 12345})
 
Remove all posts by author_id:
  ● db.blogpost.remove({'author_id': 112233})
 
Remove all blog posts:
  ● db.blogpost.remove({})
Tips
● Use small keys
  ○    a collection with 1,000,000 documents with keys like:
       {'first_name': 'Troy', 'last_name': 'Grosfield'}
       will take longer to search than a 1,000,000 documents with small
       keys such as:
       {'fn': 'Troy', 'ln': 'Grosfield'}

● Be smart with your indexes
● Design documents wisely
Hope you Enjoyed!
twitter: @troygrosfield
website: http://troygrosfield.com
blog:    http://blog.troygrosfield.com
 
Resources:
● http://mongodb.org/

More Related Content

What's hot (19)

PPTX
Node.js and angular js
HyungKuIm
 
PDF
C++ Programming - 8th Study
Chris Ohk
 
PPTX
NoSQL - Hands on
Johannes Hoppe
 
PDF
C++ Programming - 6th Study
Chris Ohk
 
KEY
Get Paid What You Are Worth
Troy Dean
 
PPTX
Basic crud operation
zarigatongy
 
KEY
Fast content import in Plone
Andrew Mleczko
 
PDF
The Ring programming language version 1.9 book - Part 50 of 210
Mahmoud Samir Fayed
 
PDF
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
Johannes Hoppe
 
PPTX
MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...
MongoDB
 
PPTX
Introduction towebmatrix
Pranav Ainavolu
 
PDF
MongoDB全機能解説2
Takahiro Inoue
 
PDF
Mongo DB schema design patterns
joergreichert
 
PDF
Lesson notes 11.03.13
Lucy Taylor
 
PDF
MongoDBで作るソーシャルデータ新解析基盤
Takahiro Inoue
 
PDF
C++ Programming - 7th Study
Chris Ohk
 
PPTX
Data Modeling for the Real World
Mike Friedman
 
PPTX
Introduction to MongoDB
Algiers Tech Meetup
 
PDF
Building Apps with MongoDB
Nate Abele
 
Node.js and angular js
HyungKuIm
 
C++ Programming - 8th Study
Chris Ohk
 
NoSQL - Hands on
Johannes Hoppe
 
C++ Programming - 6th Study
Chris Ohk
 
Get Paid What You Are Worth
Troy Dean
 
Basic crud operation
zarigatongy
 
Fast content import in Plone
Andrew Mleczko
 
The Ring programming language version 1.9 book - Part 50 of 210
Mahmoud Samir Fayed
 
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
Johannes Hoppe
 
MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...
MongoDB
 
Introduction towebmatrix
Pranav Ainavolu
 
MongoDB全機能解説2
Takahiro Inoue
 
Mongo DB schema design patterns
joergreichert
 
Lesson notes 11.03.13
Lucy Taylor
 
MongoDBで作るソーシャルデータ新解析基盤
Takahiro Inoue
 
C++ Programming - 7th Study
Chris Ohk
 
Data Modeling for the Real World
Mike Friedman
 
Introduction to MongoDB
Algiers Tech Meetup
 
Building Apps with MongoDB
Nate Abele
 

Viewers also liked (10)

PPT
Slideshow452012
mikefosmer
 
PPSX
Rohit Saxena - Sample Presentation
rohitsaxena85
 
PDF
Anleitung zum Scheitern in China – und wie seine digitale Welt dies beschleun...
TANNER GmbH
 
PPTX
Notas musicale strompeta
Daniel Bastidas Arrizalde
 
PPTX
Notas musicalesflautadulce
Daniel Bastidas Arrizalde
 
PDF
42 Antworten auf die dringenden Fragen einer immer schneller werdenden digita...
TANNER GmbH
 
PDF
E-Mail-Marketing im digitalen Transformationsprozess
TANNER GmbH
 
PPT
Topology
Aviroop Mandal
 
PDF
SEO – Technik, Struktur und Inhalt im Einklang
TANNER GmbH
 
PDF
Alles digital? Wie die digitale Transformation das Marketing verändert
TANNER GmbH
 
Slideshow452012
mikefosmer
 
Rohit Saxena - Sample Presentation
rohitsaxena85
 
Anleitung zum Scheitern in China – und wie seine digitale Welt dies beschleun...
TANNER GmbH
 
Notas musicale strompeta
Daniel Bastidas Arrizalde
 
Notas musicalesflautadulce
Daniel Bastidas Arrizalde
 
42 Antworten auf die dringenden Fragen einer immer schneller werdenden digita...
TANNER GmbH
 
E-Mail-Marketing im digitalen Transformationsprozess
TANNER GmbH
 
Topology
Aviroop Mandal
 
SEO – Technik, Struktur und Inhalt im Einklang
TANNER GmbH
 
Alles digital? Wie die digitale Transformation das Marketing verändert
TANNER GmbH
 
Ad

Similar to MongoDB NoSQL and all of its awesomeness (20)

KEY
Schema Design by Example ~ MongoSF 2012
hungarianhc
 
KEY
MongoDB
Steve Klabnik
 
PDF
Building your first app with mongo db
MongoDB
 
PPTX
Dev Jumpstart: Build Your First App with MongoDB
MongoDB
 
PDF
Pyconie 2012
Yaqi Zhao
 
KEY
Introduction to MongoDB
Neil Henegan
 
KEY
Round pegs and square holes
Daniel Greenfeld
 
KEY
MongoDB at RuPy
Mike Dirolf
 
PPTX
Introduction to MongoDB
Hossein Boustani
 
KEY
MongoDB at RubyEnRails 2009
Mike Dirolf
 
PDF
How to use MongoDB with CakePHP
ichikaway
 
KEY
MongoDB NYC Python
Mike Dirolf
 
PDF
The emerging world of mongo db csp
Carlos Sánchez Pérez
 
KEY
MongoDB Strange Loop 2009
Mike Dirolf
 
KEY
MongoDB, PHP and the cloud - php cloud summit 2011
Steven Francia
 
KEY
MongoDB at ZPUGDC
Mike Dirolf
 
PDF
Building Your First MongoDB App
Henrik Ingo
 
PPTX
Introduction to NOSQL And MongoDB
Behrouz Bakhtiari
 
PDF
10gen Presents Schema Design and Data Modeling
DATAVERSITY
 
PPT
2011 Mongo FR - MongoDB introduction
antoinegirbal
 
Schema Design by Example ~ MongoSF 2012
hungarianhc
 
MongoDB
Steve Klabnik
 
Building your first app with mongo db
MongoDB
 
Dev Jumpstart: Build Your First App with MongoDB
MongoDB
 
Pyconie 2012
Yaqi Zhao
 
Introduction to MongoDB
Neil Henegan
 
Round pegs and square holes
Daniel Greenfeld
 
MongoDB at RuPy
Mike Dirolf
 
Introduction to MongoDB
Hossein Boustani
 
MongoDB at RubyEnRails 2009
Mike Dirolf
 
How to use MongoDB with CakePHP
ichikaway
 
MongoDB NYC Python
Mike Dirolf
 
The emerging world of mongo db csp
Carlos Sánchez Pérez
 
MongoDB Strange Loop 2009
Mike Dirolf
 
MongoDB, PHP and the cloud - php cloud summit 2011
Steven Francia
 
MongoDB at ZPUGDC
Mike Dirolf
 
Building Your First MongoDB App
Henrik Ingo
 
Introduction to NOSQL And MongoDB
Behrouz Bakhtiari
 
10gen Presents Schema Design and Data Modeling
DATAVERSITY
 
2011 Mongo FR - MongoDB introduction
antoinegirbal
 
Ad

Recently uploaded (20)

PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PDF
Council of Chalcedon Re-Examined
Smiling Lungs
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PDF
epi editorial commitee meeting presentation
MIPLM
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
PDF
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
Council of Chalcedon Re-Examined
Smiling Lungs
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
epi editorial commitee meeting presentation
MIPLM
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
Controller Request and Response in Odoo18
Celine George
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 

MongoDB NoSQL and all of its awesomeness

  • 1. MongoDB and NoSQL ...and all of its awesomeness....
  • 2. What is MongoDB? ● MongoDB (from 'humongous') is a scalable, high-performance, open source NoSQL database. ● JSON-style document storage ○ actually BSON (Binary JSON) ● Awesome
  • 3. Terminology ● db > collection > document ● collection = table ● document = row ● Database is made of of collections ● Collections are made up of documents
  • 4. Why Should I Use MongoDB (Pros) ● Flexible: database changes are easy because it's a dynamic schema. ● Fast: Indexes are stored in memory which alleviates need for additional cache dependency ● Easy to scale with replica sets, sharding ● Atomic updates: All fail or all succeed  
  • 5. Why to Not Use MongoDB (Cons) ● Non transactional ● Document Size Limitations 16MB ● Ambiguous keys when shortened
  • 6. MongoDB Syntax INSERT > db.foo.insert(doc_or_docs) SELECT > db.foo.find(criteria) UPDATE > db.foo.update(criteria, updates, upsert, multi) DELETE > db.foo.remove(criteria)
  • 7. Blog Post Example ● Create a Blog Post ● Retrieve a Blog Post ● Update a Blog Post ○ $inc, $set, $unset, $push, $pushAll, $addToSet and $each, $pop, $pull, $pullAll, $rename ● Delete a Blog Post
  • 8. Define the Model class BlogPost(): In SQL world, most of you already can id see this would require 3 separate name tables: content 1. BlogPosts author_id 2. Comments   3. Tags class Comments(): author_id comment blog_id class Tags(): name blog_id
  • 9. Mongo Document Storage Structure Document structure inside of the "BlogPost" collection:   { 'id': 12345, 'name': 'Hello World', 'author_id': 112233, 'content': 'This is my blog post content. Woot woot!', 'comments': [ {'user_id': 4321, 'comment': 'Awesome blog!'}, {'user_id': 1234, 'comment': 'Totally Agree'} ], 'tags': ['hello', 'world'] }
  • 11. Demo Code: Insert doc2 = {'id': 67890, doc = {'id': 12345, 'name': 'Another Hello World', 'name': 'Hello World', 'author_id': 112233, 'author_id': 112233, 'content': 'This is my second blog post', 'content': 'This is my blog post content. Woot woot!', 'comments': [ 'comments': [ {'user_id': 4321, 'comment': 'Even better than {'user_id': 4321, 'comment': 'Awesome blog!'}, the first!'}, {'user_id': 1234, 'comment': 'Totally Agree'} {'user_id': 1234, 'comment': 'You rock'} ], ], 'tags': ['hello', 'world'] 'tags': ['jimmy', 'buffet'] } }     db.blogpost.insert(doc) db.blogpost.insert(doc2)    
  • 12. Demo Code: Retrieve Return the whole doc: ● db.blogpost.findOne() ● db.blogpost.findOne({'id': 12345})   Only return specific fields: ● db.blogpost.findOne({}, {'name':1, 'author_id':1})   Exclude specific fields: ● db.blogpost.findOne({}, {'comments': 0})   Find all blog posts by author 112233: ● db.blogpost.find({'id': 112233}).pretty()
  • 13. Demo Code: Update Update all posts by Author_id: ● db.blogpost.update({'author_id': 112233}, {'$set':{'is_featured': true}}, false, true)   Update all posts by author_id using tag "buffet": ● db.blogpost.update({'author_id': 112233, 'tags': 'buffet'}, {'$set':{'rock_star': true}}, false, true)   Update all posts by author_id where comment == 'You rock': ● db.blogpost.update({'author_id': 112233, 'comments.comment': 'You rock'}, {'$set':{'comments.$.about_rock': true}}, false, true)   Add tag to all posts by author_id: ● db.blogpost.update({'author_id': 112233}, {'$addToSet':{'tags': 'word up'}}, false, true)
  • 14. Demo Code: Delete Remove by document id: ● db.blogpost.remove({'id': 12345})   Remove all posts by author_id: ● db.blogpost.remove({'author_id': 112233})   Remove all blog posts: ● db.blogpost.remove({})
  • 15. Tips ● Use small keys ○ a collection with 1,000,000 documents with keys like: {'first_name': 'Troy', 'last_name': 'Grosfield'} will take longer to search than a 1,000,000 documents with small keys such as: {'fn': 'Troy', 'ln': 'Grosfield'} ● Be smart with your indexes ● Design documents wisely
  • 16. Hope you Enjoyed! twitter: @troygrosfield website: http://troygrosfield.com blog: http://blog.troygrosfield.com   Resources: ● http://mongodb.org/