SlideShare a Scribd company logo
Frankfurt NoSQL User Group
                                    Chris Harris
                            Email : charris@10gen.com
                               Twitter : cj_harris5



Wednesday, 22 February 12
As simple as possible,
                                        but no simpler
                                 Memcached
                                    Key / Value
     Scalability & Performance




                                                                    RDBMS




                                           Depth of functionality



Wednesday, 22 February 12
Key/Value Store
                            MongoDB
                            Relational Database




Wednesday, 22 February 12
Terminology

               RDBMS            MongoDB
               Table            Collection
               Row(s)           JSON Document
               Index            Index
               Join             Embedding & Linking
               Partition        Shard
               Partition Key    Shard Key


Wednesday, 22 February 12
Here is a “simple” SQL Model
       mysql> select * from book;
       +----+----------------------------------------------------------+
       | id | title                            |
       +----+----------------------------------------------------------+
       | 1 | The Demon-Haunted World: Science as a Candle in the Dark |
       | 2 | Cosmos                               |
       | 3 | Programming in Scala                     |
       +----+----------------------------------------------------------+
       3 rows in set (0.00 sec)

       mysql> select * from bookauthor;
       +---------+-----------+
       | book_id | author_id |
       +---------+-----------+
       |    1|       1|
       |    2|       1|
       |    3|       2|
       |    3|       3|
       |    3|       4|
       +---------+-----------+
       5 rows in set (0.00 sec)

       mysql> select * from author;
       +----+-----------+------------+-------------+-------------+---------------+
       | id | last_name | first_name | middle_name | nationality | year_of_birth |
       +----+-----------+------------+-------------+-------------+---------------+
       | 1 | Sagan    | Carl     | Edward    | NULL    | 1934         |
       | 2 | Odersky | Martin       | NULL     | DE     | 1958         |
       | 3 | Spoon    | Lex      | NULL     | NULL    | NULL         |
       | 4 | Venners | Bill      | NULL    | NULL     | NULL        |
       +----+-----------+------------+-------------+-------------+---------------+
       4 rows in set (0.00 sec)




Wednesday, 22 February 12
Flexible “Schemas”

              { “author”: “brendan”,
                “text”: “...” }

                                       { “author”: “brendan”,
                                         “text”: “...”,
                                         “tags”: [“mongodb”,
                                                “nosql”] }




Wednesday, 22 February 12
The Same Data in MongoDB

             {
           "_id" : ObjectId("4dfa6baa9c65dae09a4bbda5"),
           "title" : "Programming in Scala",
           "author" : [
               {
                  "first_name" : "Martin",
                  "last_name" : "Odersky",
                  "nationality" : "DE",
                  "year_of_birth" : 1958
               },
               {
                  "first_name" : "Lex",
                  "last_name" : "Spoon"
               },
               {
                  "first_name" : "Bill",
                  "last_name" : "Venners"
               }
           ]
       }




Wednesday, 22 February 12
CRUD Operations




Wednesday, 22 February 12
db.test.insert({fn: “Chris”, ln: “Harris”})




Wednesday, 22 February 12
db.test.find({fn: “Chris”})




Wednesday, 22 February 12
Cursors

                                 $gt, $lt, $gte, $lte, $ne, $all, $in, $nin, $or,
                               $not, $mod, $size, $exists, $type, $elemMatch


                       > var c = db.test.find({x: 20}).skip(20).limit(10)> c.next()
                       > c.next()
                       ...

                                                       query
                                             first N results + cursor id


                                               getMore w/ cursor id
                                           next N results + cursor id or 0
                                                         ...


Wednesday, 22 February 12
Creating Indexes
        An index on _id is automatic.
        For more use ensureIndex:


                     db.blogs.ensureIndex({author: 1})

                     1 = ascending
                     -1 = descending




Wednesday, 22 February 12
Compound Indexes


          db.blogs.save({
            author: "James",
            ts: new Date()
            ...
          });

          db.blogs.ensureIndex({author: 1, ts: -1})




Wednesday, 22 February 12
Unique Indexes


          db.blogs.save({
            author: "James",
            title: "My first blog"
            ...
          });

          db.blogs.ensureIndex({title: 1}, {unique: true})




Wednesday, 22 February 12
Indexing Embedded Documents
          db.blogs.save({
            title: "My First blog",
            stats : { views: 0,
                    followers: 0 }
          });

          db.blogs.ensureIndex({"stats.followers": -1})

          db.blogs.find({"stats.followers": {$gt: 500}})




Wednesday, 22 February 12
Indexing Embedded Arrays
          db.blogs.save({
            title: "My First blog",
            comments: [
              {author: "James", ts : new Date()} ]
          });

          db.blogs.ensureIndex({"comments.author": 1})

          db.blogs.find({"comments.author": "James"})




Wednesday, 22 February 12
Multikeys
          db.blogs.save({
            title: "My Second blog",
            tags: ["mongodb", "NoSQL"]
          });

          db.blogs.ensureIndex({tags: 1})

          db.blogs.find({tags: "NoSQL"})




Wednesday, 22 February 12
Covered Indexes
          • From 1.8.0
          • Query resolved in index only
          • Need to exclude _id from items projected
          db.blogs.save({
            author: "James",
            title: "My first blog"
          });

          db.blogs.ensureIndex({author: 1})

          db.blogs.find({author: "James"},
                   {author: 1, _id:0}))


Wednesday, 22 February 12
Sparse Indexes
          • From 1.8.0
          • Key value included if and only if the value is present
          • Reduces size of index
          • Limited to a single field
         db.blogs.ensureIndex({loc: 1}, {sparse: true})

         // loc key stored for Ben & Sarah only
         db.blogs.save({author: "Jim"})
         db.blogs.save({author: "Ben", loc: null})
         db.blogs.save({author: "Sarah", loc: "CA"})




Wednesday, 22 February 12
Geospatial
       • Geo hash stored in B-Tree
       • First two values indexed
          db.blogs.save({
            loc: { long: 40.739037, lat: 40.739037 }
          });

          db.blogs.save({
            loc: [40.739037, 40.739037]
          });

          db.blogs.ensureIndex({"loc": "2d"})




Wednesday, 22 February 12
Replication




Wednesday, 22 February 12
Types of outage

       • Planned
               • Hardware upgrade
               • O/S or file-system tuning
               • Relocation of data to new file-system / storage
               • Software upgrade

       • Unplanned
               • Hardware failure
               • Data center failure
               • Region outage
               • Human error
               • Application corruption
Wednesday, 22 February 12
Replica Set features
           • A cluster of N servers
           • Any (one) node can be primary
           • Consensus election of primary
           • Automatic failover
           • Automatic recovery
           • All writes to primary
           • Reads can be to primary (default) or a secondary




Wednesday, 22 February 12
How MongoDB Replication works

                            Member
                              1               Member
                                                3




                                     Member
                                       2



    •Set is made up of 2 or more nodes


Wednesday, 22 February 12
How MongoDB Replication works

                            Member
                              1                Member
                                                 3




                                     Member
                                        2
                                     PRIMARY


    •Election establishes the PRIMARY
    •Data replication from PRIMARY to SECONDARY

Wednesday, 22 February 12
How MongoDB Replication works
                                        negotiate
                                       new master
                            Member
                              1                     Member
                                                      3




                                     Member
                                       2
                                     DOWN


    •PRIMARY may fail
    •Automatic election of new PRIMARY if majority
    exists
Wednesday, 22 February 12
How MongoDB Replication works

                            Member
                              1               Member
                                                 3
                                              PRIMARY




                                     Member
                                       2
                                     DOWN


    •New PRIMARY elected
    •Replication Set re-established

Wednesday, 22 February 12
How MongoDB Replication works

                            Member
                              1                   Member
                                                     3
                                                  PRIMARY




                                     Member 2
                                     RECOVERING




    •Automatic recovery


Wednesday, 22 February 12
How MongoDB Replication works

                            Member
                              1               Member
                                                 3
                                              PRIMARY




                                     Member
                                       2



    •Replication Set re-established


Wednesday, 22 February 12
Sharding




Wednesday, 22 February 12
http://community.qlikview.com/cfs-filesystemfile.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/
                           theqlikviewblog/Cutting-Grass-with-Scissors-_2D00_-2.jpg


Wednesday, 22 February 12
http://www.bitquill.net/blog/wp-content/uploads/2008/07/pack_of_harvesters.jpg

Wednesday, 22 February 12
MongoDB Scaling - Single Node
        read




                            node_a1




                                      write


Wednesday, 22 February 12
Write scaling - add Shards
        read


                            shard1    shard2


                            node_c1   node_c2


                            node_b1   node_b2


                            node_a1   node_a2




                                                write


Wednesday, 22 February 12
Write scaling - add Shards
        read


                            shard1    shard2    shard3


                            node_c1   node_c2   node_c3


                            node_b1   node_b2   node_b3


                            node_a1   node_a2   node_a3




                                                          write


Wednesday, 22 February 12
MongoDB Sharding

       • Automatic partitioning and management
       • Range based
       • Convert to sharded system with no downtime
       • Fully consistent




Wednesday, 22 February 12
How MongoDB Sharding works

       > db.posts.save( {age:40} )



                            -∞   +∞  

             -∞   40              41 +∞  



    •Data in inserted
    •Ranges are split into more “chunks”
Wednesday, 22 February 12
How MongoDB Sharding works

       > db.posts.save( {age:40} )
       > db.posts.save( {age:50} )


                            -∞   +∞  

             -∞   40              41 +∞  

                             41   50    51 +∞  

    •More Data in inserted
    •Ranges are split into more“chunks”
Wednesday, 22 February 12
How MongoDB Sharding works

       > db.posts.save( {age:40} )
       > db.posts.save( {age:50} )
       > db.posts.save( {age:60} )

                            -∞   +∞  

             -∞   40              41 +∞  

                             41   50    51 +∞  

                                   51   60   61 +∞  


Wednesday, 22 February 12
Balancing
                                                          mongos
                                                                                                      config
                                                          balancer
                                                                                                      config
              Chunks!
                                                                                                      config




                  1         2   3    4    13    14   15   16         25    26   27   28   37    38   39   40

                  5         6   7    8    17    18   19   20         29    30   31   32   41    42   43   44

                  9     10      11   12   21    22   23   24         33    34   35   36   45    46   47   48


                      Shard 1                  Shard 2                    Shard 3              Shard 4




Wednesday, 22 February 12
Balancing
                                                            mongos
                                                                                                        config
                                                            balancer
                                                                                                        config


                                          Imbalance
                                           Imbalance                                                    config




                  1         2   3    4

                  5         6   7    8

                  9     10      11   12     21    22   23   24         33    34   35   36   45    46   47   48


                      Shard 1                    Shard 2                    Shard 3              Shard 4




Wednesday, 22 February 12
Balancing
                                                          mongos
                                                                                                      config
                                                          balancer
                                                                                                      config

                                                 Move chunk 1 to                                      config
                                                 Shard 2




                  1         2   3    4

                  5         6   7    8

                  9     10      11   12   21    22   23   24         33    34   35   36   45    46   47   48


                      Shard 1                  Shard 2                    Shard 3              Shard 4




Wednesday, 22 February 12
Balancing
                                                          mongos
                                                                                                      config
                                                          balancer
                                                                                                      config

                                                                                                      config




                  1         2   3    4

                  5         6   7    8

                  9     10      11   12   21    22   23   24         33    34   35   36   45    46   47   48


                      Shard 1                  Shard 2                    Shard 3              Shard 4




Wednesday, 22 February 12
Balancing
                                                          mongos
                                                                                                      config
                                                          balancer
                                                                                                      config

                                                                                                      config




                            2   3    4

                  5         6   7    8    1

                  9     10      11   12   21    22   23   24         33    34   35   36   45    46   47   48


                      Shard 1                  Shard 2                    Shard 3              Shard 4




Wednesday, 22 February 12
Balancing
                                                          mongos
                                                                                                      config
                                                          balancer
                                                                                                      config
                                                               Chunk 1 now lives on
                                                                    Shard 2
                                                                                                      config




                            2   3    4

                  5         6   7    8    1

                  9     10      11   12   21    22   23   24         33    34   35   36   45    46   47   48


                      Shard 1                  Shard 2                    Shard 3              Shard 4




Wednesday, 22 February 12

More Related Content

PPTX
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
PDF
MongoDB .local Munich 2019: New Encryption Capabilities in MongoDB 4.2: A Dee...
PDF
Optimizing Slow Queries with Indexes and Creativity
PPTX
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
PDF
Map/Confused? A practical approach to Map/Reduce with MongoDB
PPTX
MongoDB + Java - Everything you need to know
PPTX
Mythbusting: Understanding How We Measure the Performance of MongoDB
PPTX
Back to Basics Webinar 3: Schema Design Thinking in Documents
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
MongoDB .local Munich 2019: New Encryption Capabilities in MongoDB 4.2: A Dee...
Optimizing Slow Queries with Indexes and Creativity
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
Map/Confused? A practical approach to Map/Reduce with MongoDB
MongoDB + Java - Everything you need to know
Mythbusting: Understanding How We Measure the Performance of MongoDB
Back to Basics Webinar 3: Schema Design Thinking in Documents

What's hot (20)

PPTX
Indexing Strategies to Help You Scale
PPTX
Webinar: Back to Basics: Thinking in Documents
PDF
Pinterest的数据库分片架构
PPTX
Building a Scalable Inbox System with MongoDB and Java
PDF
MongoDB .local Chicago 2019: Using Client Side Encryption in MongoDB 4.2
PDF
Building Apps with MongoDB
PDF
MongoDB Performance Tuning
PPTX
MongoDB Schema Design: Four Real-World Examples
PDF
MongoDB Schema Design
PPTX
Webinar: Schema Design
PDF
Building a Social Network with MongoDB
PPTX
Dev Jumpstart: Schema Design Best Practices
PDF
Indexing
KEY
Schema Design with MongoDB
PDF
Storing tree structures with MongoDB
PDF
MongoDB .local Munich 2019: Best Practices for Working with IoT and Time-seri...
PPTX
Choosing a Shard key
PDF
MongoDB .local Houston 2019: Using Client Side Encryption in MongoDB 4.2
PPT
5 Pitfalls to Avoid with MongoDB
PDF
MongoDB dessi-codemotion
Indexing Strategies to Help You Scale
Webinar: Back to Basics: Thinking in Documents
Pinterest的数据库分片架构
Building a Scalable Inbox System with MongoDB and Java
MongoDB .local Chicago 2019: Using Client Side Encryption in MongoDB 4.2
Building Apps with MongoDB
MongoDB Performance Tuning
MongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design
Webinar: Schema Design
Building a Social Network with MongoDB
Dev Jumpstart: Schema Design Best Practices
Indexing
Schema Design with MongoDB
Storing tree structures with MongoDB
MongoDB .local Munich 2019: Best Practices for Working with IoT and Time-seri...
Choosing a Shard key
MongoDB .local Houston 2019: Using Client Side Encryption in MongoDB 4.2
5 Pitfalls to Avoid with MongoDB
MongoDB dessi-codemotion
Ad

Viewers also liked (20)

PDF
How to install the zwave plugin switch controller
PPT
Don eppelheimer, trane
PDF
How to Infuse Your Media Planning with Social Data - by Forrester & Networked...
PPS
Ovos trabalhados arte na casca de ovos
PDF
Fraudes digitais no mundo pós-SPED - ENECON - 17.6.2011
PPT
Quinto a
KEY
Introduction to Ext JS 4
PDF
Pesquisa AvançAda Na Internet 2009
DOCX
Infolitigpart1
PDF
Curso de marketing em mídias sociais
PDF
Adobe Digital Publishing Suite by dualpixel
PPT
00 a linguagem html
PDF
Daron Yöndem - ie8 Ebook Tr
PDF
Ijm 06 10_012
PDF
Comparative analysis on E-Gov web sites
PDF
Java E
PPT
Web Application Hacking 2004
PPT
Unemployment
PDF
JSF2 and JSP
PDF
document
How to install the zwave plugin switch controller
Don eppelheimer, trane
How to Infuse Your Media Planning with Social Data - by Forrester & Networked...
Ovos trabalhados arte na casca de ovos
Fraudes digitais no mundo pós-SPED - ENECON - 17.6.2011
Quinto a
Introduction to Ext JS 4
Pesquisa AvançAda Na Internet 2009
Infolitigpart1
Curso de marketing em mídias sociais
Adobe Digital Publishing Suite by dualpixel
00 a linguagem html
Daron Yöndem - ie8 Ebook Tr
Ijm 06 10_012
Comparative analysis on E-Gov web sites
Java E
Web Application Hacking 2004
Unemployment
JSF2 and JSP
document
Ad

Similar to MongoDB @ Frankfurt NoSql User Group (20)

PDF
MongoSV Schema Workshop
PDF
De normalised london aggregation framework overview
PDF
Intro To MongoDB
PDF
Latinoware
PDF
Mongo db
PDF
10gen Presents Schema Design and Data Modeling
PPTX
JS App Architecture
KEY
Modeling Data in MongoDB
PDF
Node-IL Meetup 12/2
PPTX
Data Modeling for NoSQL
PDF
Introduction To MongoDB
KEY
Schema design
KEY
2012 phoenix mug
PDF
Relationships are hard
KEY
Mongo db勉強会20110730
KEY
Schema Design (Mongo Austin)
PPTX
Mongodb introduction and_internal(simple)
PDF
An Overview of Data Management Paradigms: Relational, Document, and Graph
KEY
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
PDF
Getting Started With MongoDB and Mongoose
MongoSV Schema Workshop
De normalised london aggregation framework overview
Intro To MongoDB
Latinoware
Mongo db
10gen Presents Schema Design and Data Modeling
JS App Architecture
Modeling Data in MongoDB
Node-IL Meetup 12/2
Data Modeling for NoSQL
Introduction To MongoDB
Schema design
2012 phoenix mug
Relationships are hard
Mongo db勉強会20110730
Schema Design (Mongo Austin)
Mongodb introduction and_internal(simple)
An Overview of Data Management Paradigms: Relational, Document, and Graph
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
Getting Started With MongoDB and Mongoose

Recently uploaded (20)

PDF
Approach and Philosophy of On baking technology
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Encapsulation theory and applications.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
KodekX | Application Modernization Development
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
DOCX
The AUB Centre for AI in Media Proposal.docx
PPT
Teaching material agriculture food technology
PPTX
sap open course for s4hana steps from ECC to s4
Approach and Philosophy of On baking technology
Encapsulation_ Review paper, used for researhc scholars
Dropbox Q2 2025 Financial Results & Investor Presentation
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Encapsulation theory and applications.pdf
Chapter 3 Spatial Domain Image Processing.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
KodekX | Application Modernization Development
Advanced methodologies resolving dimensionality complications for autism neur...
“AI and Expert System Decision Support & Business Intelligence Systems”
The Rise and Fall of 3GPP – Time for a Sabbatical?
Review of recent advances in non-invasive hemoglobin estimation
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
The AUB Centre for AI in Media Proposal.docx
Teaching material agriculture food technology
sap open course for s4hana steps from ECC to s4

MongoDB @ Frankfurt NoSql User Group

  • 1. Frankfurt NoSQL User Group Chris Harris Email : charris@10gen.com Twitter : cj_harris5 Wednesday, 22 February 12
  • 2. As simple as possible, but no simpler Memcached Key / Value Scalability & Performance RDBMS Depth of functionality Wednesday, 22 February 12
  • 3. Key/Value Store MongoDB Relational Database Wednesday, 22 February 12
  • 4. Terminology RDBMS MongoDB Table Collection Row(s) JSON Document Index Index Join Embedding & Linking Partition Shard Partition Key Shard Key Wednesday, 22 February 12
  • 5. Here is a “simple” SQL Model mysql> select * from book; +----+----------------------------------------------------------+ | id | title | +----+----------------------------------------------------------+ | 1 | The Demon-Haunted World: Science as a Candle in the Dark | | 2 | Cosmos | | 3 | Programming in Scala | +----+----------------------------------------------------------+ 3 rows in set (0.00 sec) mysql> select * from bookauthor; +---------+-----------+ | book_id | author_id | +---------+-----------+ | 1| 1| | 2| 1| | 3| 2| | 3| 3| | 3| 4| +---------+-----------+ 5 rows in set (0.00 sec) mysql> select * from author; +----+-----------+------------+-------------+-------------+---------------+ | id | last_name | first_name | middle_name | nationality | year_of_birth | +----+-----------+------------+-------------+-------------+---------------+ | 1 | Sagan | Carl | Edward | NULL | 1934 | | 2 | Odersky | Martin | NULL | DE | 1958 | | 3 | Spoon | Lex | NULL | NULL | NULL | | 4 | Venners | Bill | NULL | NULL | NULL | +----+-----------+------------+-------------+-------------+---------------+ 4 rows in set (0.00 sec) Wednesday, 22 February 12
  • 6. Flexible “Schemas” { “author”: “brendan”, “text”: “...” } { “author”: “brendan”, “text”: “...”, “tags”: [“mongodb”, “nosql”] } Wednesday, 22 February 12
  • 7. The Same Data in MongoDB { "_id" : ObjectId("4dfa6baa9c65dae09a4bbda5"), "title" : "Programming in Scala", "author" : [ { "first_name" : "Martin", "last_name" : "Odersky", "nationality" : "DE", "year_of_birth" : 1958 }, { "first_name" : "Lex", "last_name" : "Spoon" }, { "first_name" : "Bill", "last_name" : "Venners" } ] } Wednesday, 22 February 12
  • 9. db.test.insert({fn: “Chris”, ln: “Harris”}) Wednesday, 22 February 12
  • 11. Cursors $gt, $lt, $gte, $lte, $ne, $all, $in, $nin, $or, $not, $mod, $size, $exists, $type, $elemMatch > var c = db.test.find({x: 20}).skip(20).limit(10)> c.next() > c.next() ... query first N results + cursor id getMore w/ cursor id next N results + cursor id or 0 ... Wednesday, 22 February 12
  • 12. Creating Indexes An index on _id is automatic. For more use ensureIndex: db.blogs.ensureIndex({author: 1}) 1 = ascending -1 = descending Wednesday, 22 February 12
  • 13. Compound Indexes db.blogs.save({ author: "James", ts: new Date() ... }); db.blogs.ensureIndex({author: 1, ts: -1}) Wednesday, 22 February 12
  • 14. Unique Indexes db.blogs.save({ author: "James", title: "My first blog" ... }); db.blogs.ensureIndex({title: 1}, {unique: true}) Wednesday, 22 February 12
  • 15. Indexing Embedded Documents db.blogs.save({ title: "My First blog", stats : { views: 0, followers: 0 } }); db.blogs.ensureIndex({"stats.followers": -1}) db.blogs.find({"stats.followers": {$gt: 500}}) Wednesday, 22 February 12
  • 16. Indexing Embedded Arrays db.blogs.save({ title: "My First blog", comments: [ {author: "James", ts : new Date()} ] }); db.blogs.ensureIndex({"comments.author": 1}) db.blogs.find({"comments.author": "James"}) Wednesday, 22 February 12
  • 17. Multikeys db.blogs.save({ title: "My Second blog", tags: ["mongodb", "NoSQL"] }); db.blogs.ensureIndex({tags: 1}) db.blogs.find({tags: "NoSQL"}) Wednesday, 22 February 12
  • 18. Covered Indexes • From 1.8.0 • Query resolved in index only • Need to exclude _id from items projected db.blogs.save({ author: "James", title: "My first blog" }); db.blogs.ensureIndex({author: 1}) db.blogs.find({author: "James"}, {author: 1, _id:0})) Wednesday, 22 February 12
  • 19. Sparse Indexes • From 1.8.0 • Key value included if and only if the value is present • Reduces size of index • Limited to a single field db.blogs.ensureIndex({loc: 1}, {sparse: true}) // loc key stored for Ben & Sarah only db.blogs.save({author: "Jim"}) db.blogs.save({author: "Ben", loc: null}) db.blogs.save({author: "Sarah", loc: "CA"}) Wednesday, 22 February 12
  • 20. Geospatial • Geo hash stored in B-Tree • First two values indexed db.blogs.save({ loc: { long: 40.739037, lat: 40.739037 } }); db.blogs.save({ loc: [40.739037, 40.739037] }); db.blogs.ensureIndex({"loc": "2d"}) Wednesday, 22 February 12
  • 22. Types of outage • Planned • Hardware upgrade • O/S or file-system tuning • Relocation of data to new file-system / storage • Software upgrade • Unplanned • Hardware failure • Data center failure • Region outage • Human error • Application corruption Wednesday, 22 February 12
  • 23. Replica Set features • A cluster of N servers • Any (one) node can be primary • Consensus election of primary • Automatic failover • Automatic recovery • All writes to primary • Reads can be to primary (default) or a secondary Wednesday, 22 February 12
  • 24. How MongoDB Replication works Member 1 Member 3 Member 2 •Set is made up of 2 or more nodes Wednesday, 22 February 12
  • 25. How MongoDB Replication works Member 1 Member 3 Member 2 PRIMARY •Election establishes the PRIMARY •Data replication from PRIMARY to SECONDARY Wednesday, 22 February 12
  • 26. How MongoDB Replication works negotiate new master Member 1 Member 3 Member 2 DOWN •PRIMARY may fail •Automatic election of new PRIMARY if majority exists Wednesday, 22 February 12
  • 27. How MongoDB Replication works Member 1 Member 3 PRIMARY Member 2 DOWN •New PRIMARY elected •Replication Set re-established Wednesday, 22 February 12
  • 28. How MongoDB Replication works Member 1 Member 3 PRIMARY Member 2 RECOVERING •Automatic recovery Wednesday, 22 February 12
  • 29. How MongoDB Replication works Member 1 Member 3 PRIMARY Member 2 •Replication Set re-established Wednesday, 22 February 12
  • 31. http://community.qlikview.com/cfs-filesystemfile.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/ theqlikviewblog/Cutting-Grass-with-Scissors-_2D00_-2.jpg Wednesday, 22 February 12
  • 33. MongoDB Scaling - Single Node read node_a1 write Wednesday, 22 February 12
  • 34. Write scaling - add Shards read shard1 shard2 node_c1 node_c2 node_b1 node_b2 node_a1 node_a2 write Wednesday, 22 February 12
  • 35. Write scaling - add Shards read shard1 shard2 shard3 node_c1 node_c2 node_c3 node_b1 node_b2 node_b3 node_a1 node_a2 node_a3 write Wednesday, 22 February 12
  • 36. MongoDB Sharding • Automatic partitioning and management • Range based • Convert to sharded system with no downtime • Fully consistent Wednesday, 22 February 12
  • 37. How MongoDB Sharding works > db.posts.save( {age:40} ) -∞   +∞   -∞   40 41 +∞   •Data in inserted •Ranges are split into more “chunks” Wednesday, 22 February 12
  • 38. How MongoDB Sharding works > db.posts.save( {age:40} ) > db.posts.save( {age:50} ) -∞   +∞   -∞   40 41 +∞   41 50 51 +∞   •More Data in inserted •Ranges are split into more“chunks” Wednesday, 22 February 12
  • 39. How MongoDB Sharding works > db.posts.save( {age:40} ) > db.posts.save( {age:50} ) > db.posts.save( {age:60} ) -∞   +∞   -∞   40 41 +∞   41 50 51 +∞   51 60 61 +∞   Wednesday, 22 February 12
  • 40. Balancing mongos config balancer config Chunks! config 1 2 3 4 13 14 15 16 25 26 27 28 37 38 39 40 5 6 7 8 17 18 19 20 29 30 31 32 41 42 43 44 9 10 11 12 21 22 23 24 33 34 35 36 45 46 47 48 Shard 1 Shard 2 Shard 3 Shard 4 Wednesday, 22 February 12
  • 41. Balancing mongos config balancer config Imbalance Imbalance config 1 2 3 4 5 6 7 8 9 10 11 12 21 22 23 24 33 34 35 36 45 46 47 48 Shard 1 Shard 2 Shard 3 Shard 4 Wednesday, 22 February 12
  • 42. Balancing mongos config balancer config Move chunk 1 to config Shard 2 1 2 3 4 5 6 7 8 9 10 11 12 21 22 23 24 33 34 35 36 45 46 47 48 Shard 1 Shard 2 Shard 3 Shard 4 Wednesday, 22 February 12
  • 43. Balancing mongos config balancer config config 1 2 3 4 5 6 7 8 9 10 11 12 21 22 23 24 33 34 35 36 45 46 47 48 Shard 1 Shard 2 Shard 3 Shard 4 Wednesday, 22 February 12
  • 44. Balancing mongos config balancer config config 2 3 4 5 6 7 8 1 9 10 11 12 21 22 23 24 33 34 35 36 45 46 47 48 Shard 1 Shard 2 Shard 3 Shard 4 Wednesday, 22 February 12
  • 45. Balancing mongos config balancer config Chunk 1 now lives on Shard 2 config 2 3 4 5 6 7 8 1 9 10 11 12 21 22 23 24 33 34 35 36 45 46 47 48 Shard 1 Shard 2 Shard 3 Shard 4 Wednesday, 22 February 12