SlideShare una empresa de Scribd logo
1 de 16
Descargar para leer sin conexión
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/

Más contenido relacionado

La actualidad más candente

Fast content import in Plone
Fast content import in PloneFast content import in Plone
Fast content import in Plone
Andrew Mleczko
 
MongoDB全機能解説2
MongoDB全機能解説2MongoDB全機能解説2
MongoDB全機能解説2
Takahiro Inoue
 
Lesson notes 11.03.13
Lesson notes 11.03.13Lesson notes 11.03.13
Lesson notes 11.03.13
Lucy Taylor
 
MongoDBで作るソーシャルデータ新解析基盤
MongoDBで作るソーシャルデータ新解析基盤MongoDBで作るソーシャルデータ新解析基盤
MongoDBで作るソーシャルデータ新解析基盤
Takahiro Inoue
 

La actualidad más candente (19)

Node.js and angular js
Node.js and angular jsNode.js and angular js
Node.js and angular js
 
C++ Programming - 8th Study
C++ Programming - 8th StudyC++ Programming - 8th Study
C++ Programming - 8th Study
 
NoSQL - Hands on
NoSQL - Hands onNoSQL - Hands on
NoSQL - Hands on
 
C++ Programming - 6th Study
C++ Programming - 6th StudyC++ Programming - 6th Study
C++ Programming - 6th Study
 
Get Paid What You Are Worth
Get Paid What You Are WorthGet Paid What You Are Worth
Get Paid What You Are Worth
 
Basic crud operation
Basic crud operationBasic crud operation
Basic crud operation
 
Fast content import in Plone
Fast content import in PloneFast content import in Plone
Fast content import in Plone
 
The Ring programming language version 1.9 book - Part 50 of 210
The Ring programming language version 1.9 book - Part 50 of 210The Ring programming language version 1.9 book - Part 50 of 210
The Ring programming language version 1.9 book - Part 50 of 210
 
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
 
MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...
MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...
MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...
 
Introduction towebmatrix
Introduction towebmatrixIntroduction towebmatrix
Introduction towebmatrix
 
MongoDB全機能解説2
MongoDB全機能解説2MongoDB全機能解説2
MongoDB全機能解説2
 
Mongo DB schema design patterns
Mongo DB schema design patternsMongo DB schema design patterns
Mongo DB schema design patterns
 
Lesson notes 11.03.13
Lesson notes 11.03.13Lesson notes 11.03.13
Lesson notes 11.03.13
 
MongoDBで作るソーシャルデータ新解析基盤
MongoDBで作るソーシャルデータ新解析基盤MongoDBで作るソーシャルデータ新解析基盤
MongoDBで作るソーシャルデータ新解析基盤
 
C++ Programming - 7th Study
C++ Programming - 7th StudyC++ Programming - 7th Study
C++ Programming - 7th Study
 
Data Modeling for the Real World
Data Modeling for the Real WorldData Modeling for the Real World
Data Modeling for the Real World
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
Building Apps with MongoDB
Building Apps with MongoDBBuilding Apps with MongoDB
Building Apps with MongoDB
 

Destacado

Rohit Saxena - Sample Presentation
Rohit Saxena - Sample PresentationRohit Saxena - Sample Presentation
Rohit Saxena - Sample Presentation
rohitsaxena85
 

Destacado (10)

Slideshow452012
Slideshow452012Slideshow452012
Slideshow452012
 
Rohit Saxena - Sample Presentation
Rohit Saxena - Sample PresentationRohit Saxena - Sample Presentation
Rohit Saxena - Sample Presentation
 
Anleitung zum Scheitern in China – und wie seine digitale Welt dies beschleun...
Anleitung zum Scheitern in China – und wie seine digitale Welt dies beschleun...Anleitung zum Scheitern in China – und wie seine digitale Welt dies beschleun...
Anleitung zum Scheitern in China – und wie seine digitale Welt dies beschleun...
 
Notas musicale strompeta
Notas musicale strompetaNotas musicale strompeta
Notas musicale strompeta
 
Notas musicalesflautadulce
Notas musicalesflautadulceNotas musicalesflautadulce
Notas musicalesflautadulce
 
42 Antworten auf die dringenden Fragen einer immer schneller werdenden digita...
42 Antworten auf die dringenden Fragen einer immer schneller werdenden digita...42 Antworten auf die dringenden Fragen einer immer schneller werdenden digita...
42 Antworten auf die dringenden Fragen einer immer schneller werdenden digita...
 
E-Mail-Marketing im digitalen Transformationsprozess
E-Mail-Marketing im digitalen TransformationsprozessE-Mail-Marketing im digitalen Transformationsprozess
E-Mail-Marketing im digitalen Transformationsprozess
 
Topology
TopologyTopology
Topology
 
SEO – Technik, Struktur und Inhalt im Einklang
SEO – Technik, Struktur und Inhalt im EinklangSEO – Technik, Struktur und Inhalt im Einklang
SEO – Technik, Struktur und Inhalt im Einklang
 
Alles digital? Wie die digitale Transformation das Marketing verändert
Alles digital? Wie die digitale Transformation das Marketing verändertAlles digital? Wie die digitale Transformation das Marketing verändert
Alles digital? Wie die digitale Transformation das Marketing verändert
 

Similar a MongoDB NoSQL and all of its awesomeness

Kicking ass with redis
Kicking ass with redisKicking ass with redis
Kicking ass with redis
Dvir Volk
 
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
 
Scaling MongoDB; Sharding Into and Beyond the Multi-Terabyte Range
Scaling MongoDB; Sharding Into and Beyond the Multi-Terabyte RangeScaling MongoDB; Sharding Into and Beyond the Multi-Terabyte Range
Scaling MongoDB; Sharding Into and Beyond the Multi-Terabyte Range
MongoDB
 
Marc s01 e02-crud-database
Marc s01 e02-crud-databaseMarc s01 e02-crud-database
Marc s01 e02-crud-database
MongoDB
 
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
MongoDB
 
Database madness with_mongoengine_and_sql_alchemy
Database madness with_mongoengine_and_sql_alchemyDatabase madness with_mongoengine_and_sql_alchemy
Database madness with_mongoengine_and_sql_alchemy
Jaime Buelta
 
2012 mongo db_bangalore_roadmap_new
2012 mongo db_bangalore_roadmap_new2012 mongo db_bangalore_roadmap_new
2012 mongo db_bangalore_roadmap_new
MongoDB
 
MongoDB - A Document NoSQL Database
MongoDB - A Document NoSQL DatabaseMongoDB - A Document NoSQL Database
MongoDB - A Document NoSQL Database
Ruben Inoto Soto
 

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

Kicking ass with redis
Kicking ass with redisKicking ass with redis
Kicking ass with redis
 
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
 
Scaling MongoDB; Sharding Into and Beyond the Multi-Terabyte Range
Scaling MongoDB; Sharding Into and Beyond the Multi-Terabyte RangeScaling MongoDB; Sharding Into and Beyond the Multi-Terabyte Range
Scaling MongoDB; Sharding Into and Beyond the Multi-Terabyte Range
 
Round pegs and square holes
Round pegs and square holesRound pegs and square holes
Round pegs and square holes
 
Data normalization & memoized denormalization
Data normalization & memoized denormalizationData normalization & memoized denormalization
Data normalization & memoized denormalization
 
Typowanie nominalne w TypeScript
Typowanie nominalne w TypeScriptTypowanie nominalne w TypeScript
Typowanie nominalne w TypeScript
 
Back to Basics Webinar 3: Schema Design Thinking in Documents
 Back to Basics Webinar 3: Schema Design Thinking in Documents Back to Basics Webinar 3: Schema Design Thinking in Documents
Back to Basics Webinar 3: Schema Design Thinking in Documents
 
Montreal Sql saturday: moving data from no sql db to azure data lake
Montreal Sql saturday: moving data from no sql db to azure data lakeMontreal Sql saturday: moving data from no sql db to azure data lake
Montreal Sql saturday: moving data from no sql db to azure data lake
 
How MySQL can boost (or kill) your application
How MySQL can boost (or kill) your applicationHow MySQL can boost (or kill) your application
How MySQL can boost (or kill) your 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
 
Marc s01 e02-crud-database
Marc s01 e02-crud-databaseMarc s01 e02-crud-database
Marc s01 e02-crud-database
 
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
 
Database madness with_mongoengine_and_sql_alchemy
Database madness with_mongoengine_and_sql_alchemyDatabase madness with_mongoengine_and_sql_alchemy
Database madness with_mongoengine_and_sql_alchemy
 
MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011
 
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
 
ActiveRecord vs Mongoid
ActiveRecord vs MongoidActiveRecord vs Mongoid
ActiveRecord vs Mongoid
 
2013-03-23 - NoSQL Spartakiade
2013-03-23 - NoSQL Spartakiade2013-03-23 - NoSQL Spartakiade
2013-03-23 - NoSQL Spartakiade
 
Schema Design by Example ~ MongoSF 2012
Schema Design by Example ~ MongoSF 2012Schema Design by Example ~ MongoSF 2012
Schema Design by Example ~ MongoSF 2012
 
2012 mongo db_bangalore_roadmap_new
2012 mongo db_bangalore_roadmap_new2012 mongo db_bangalore_roadmap_new
2012 mongo db_bangalore_roadmap_new
 
MongoDB - A Document NoSQL Database
MongoDB - A Document NoSQL DatabaseMongoDB - A Document NoSQL Database
MongoDB - A Document NoSQL Database
 

Último

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Último (20)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 

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/