Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Welcome to MongoDB

Norberto
October 15, 2013

Welcome to MongoDB

Presentation on MongoDB basics presented at http://www.meetup.com/Oporto-mongoDB-User-Group/events/139839692/

Norberto

October 15, 2013
Tweet

More Decks by Norberto

Other Decks in Technology

Transcript

  1. Who am I •  Norberto •  Solutions Architect •  Barcelona

    (on my way to Brussels) •  I like databases
  2. Relational Databases •  Been around for a while –  First

    paper to be published on relational db back to 1970 –  http://dl.acm.org/citation.cfm?doid=362384.362685 •  Lot’s of different tech based on SQL •  Default standard for persisting information
  3. RDBMS Scale = Bigger Computers “Clients can also opt to

    run zEC12 without a raised datacenter floor -- a first for high-end IBM mainframes.” IBM Press Release 28 Aug, 2012
  4. Key -> Value •  One-dimensional storage •  Single value is

    a blob •  Query on key only •  No schema •  No updates just replaces Key Blob
  5. Relational •  2-dimensional storage (tuples) •  Each field contains one

    value •  Query on any field •  Very structured schema (tables) •  In-place updates •  Normalization requires many tables, joins, indexes •  Poor data locality Primary Key
  6. Document •  N-dimensional storage •  Each field can contain 0,1,

    many or embedded values •  Query on any field & level •  Flexible schema •  Inline updates •  Embedding related data has optimal data locality, requires fewer indexes, has better performance _id
  7. NoSQL Databases •  Key Value Stores –  Redis •  Tabular

    –  Cassandra –  Hbase –  CouchBase •  Document –  MongoDB –  CouchDB •  Graph –  OrientDB –  Neo4J
  8. Terminology RDBMS MongoDB Table, View ➜ Collection Row ➜ Document

    Index ➜ Index Join ➜ Embedded Document Foreign Key ➜ Reference Partition ➜ Shard
  9. Typical (relational) ERD User ·Name ·Email address Category ·Name ·URL

    Comment ·Comment ·Date ·Author Article ·Name ·Slug ·Publish date ·Text Tag ·Name ·URL
  10. MongoDB ERD User ·Name ·Email address Article ·Name ·Slug ·Publish

    date ·Text ·Author Comment[] ·Comment ·Date ·Author Tag[] ·Value Category[] ·Value
  11. MongoDB is a ___________ database •  Document •  Open source

    •  High performance •  Horizontally scalable •  Full featured
  12. 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 •  etc
  13. Open Source •  MongoDB is an open source project • 

    On GitHub •  Licensed under the AGPL •  Started & sponsored by 10gen •  Commercial licenses available •  Contributions welcome
  14. 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
  15. Full Featured •  Ad Hoc queries •  Real time aggregation

    •  Rich query capabilities •  Strongly consistent •  Geospatial features •  Support for most programming languages •  Flexible schema
  16. MacBook-Air-:~ $ mongo MongoDB shell version: 2.4.4 connecting to: test

    > db.test.insert({text: 'Welcome to MongoDB'}) > db.test.find().pretty() { "_id" : ObjectId("51c34130fbd5d7261b4cdb55"), "text" : "Welcome to MongoDB" } Mongo Shell
  17. >db test > use blog switching to db blog >

    db.users.insert( user ) Switch to Your DB
  18. _id •  _id is the primary key in MongoDB • 

    Automatically indexed •  Automatically created as an ObjectId if not provided •  Any unique immutable value could be used
  19. ObjectId •  ObjectId is a special 12 byte value • 

    Guaranteed to be unique across your cluster •  ObjectId("50804d0bd94ccab2da652599") |----ts-----||---mac---||-pid-||----inc-----| 4 3 2 3
  20. > db.article.insert({ title: ‘Hello World’, body: ‘This is my first

    blog post’, date: new Date(‘2013-06-20’), username: ‘erlichson’, tags: [‘adventure’, ‘mongodb’], comments: [ ] }) Creating a Blog Post
  21. > db.article.find().pretty() { "_id" : ObjectId("51c3bafafbd5d7261b4cdb5a"), "title" : "Hello World",

    "body" : "This is my first blog post", "date" : ISODate("2013-06-20T00:00:00Z"), "username" : "erlichson", "tags" : [ "adventure", "mongodb" ], "comments" : [ ] } Finding the Post
  22. > db.article.find({tags:'adventure'}).pretty() { "_id" : ObjectId("51c3bcddfbd5d7261b4cdb5b"), "title" : "Hello World",

    "body" : "This is my first blog post", "date" : ISODate("2013-06-20T00:00:00Z"), "username" : "erlichson", "tags" : [ "adventure", "mongodb" ], "comments" : [ ] } Querying An Array
  23. > db.article.findOne({_id: new ObjectId("51c3bcddfbd5d7261b4cdb5b")}) { "_id" : ObjectId("51c3bcddfbd5d7261b4cdb5b"), "body" :

    "This is my first blog post", "comments" : [ { "name" : "Steve Blank", "comment" : "Awesome Post" } ], "date" : ISODate("2013-06-20T00:00:00Z"), "tags" : [ "adventure", "mongodb" ], "title" : "Hello World", "username" : "erlichson" } Post with Comment Attached
  24. Node 1 Secondary Config Server Node 1 Secondary Config Server

    Node 1 Secondary Config Server Shard Shard Shard Mongos App Server Mongos App Server Mongos App Server Sharding infrastructure
  25. QA