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

Thomas Rückstieß - Building your first app; an ...

Thomas Rückstieß - Building your first app; an Introduction to MongoDB

This talk will introduce the philosophy and features of the open source, NoSQL database MongoDB. We’ll discuss the benefits of the document-based data model that MongoDB offers by walking through how one can build a simple app to store books. We’ll cover inserting, updating, and querying the database of books. The second part of the talk will then dive a little deeper into advanced topics, like schema design, query optimization and indexing.

MunichDataGeeks

October 08, 2013
Tweet

More Decks by MunichDataGeeks

Other Decks in Technology

Transcript

  1. MongoDB is a ___________ database •  Document •  Open source

    •  High performance •  Horizontally scalable •  Full featured
  2. Document Database •  Not for .PDF & .DOC files • 

    A document is essentially an associative array with key/value pairs (possibly nested) { username : “thomas.r", num_logins : 39, last_login : ISODate("2013-10-08T16:46:21Z"), permissions : ["read", "write", "list", "admin"], company : { name : "MongoDB, Inc." location : "Sydney, Australia" } }
  3. Open Source •  MongoDB is an open source project • 

    On GitHub •  Licensed under the AGPL •  Started & sponsored by 10gen (now MongoDB, Inc.) •  Commercial licenses available •  Contributions welcome
  4. High Performance •  Written in C++ •  Data serialized as

    BSON (fast parsing) •  Extensive use of memory-mapped files i.e. read-through write-through memory caching. •  Full support for primary & secondary indexes •  Document model = less work
  5. Full Featured •  Ad Hoc queries •  Real time aggregation

    •  Rich query capabilities •  Strongly consistent •  Geospatial features •  Support for most programming languages •  Flexible schema
  6. $ 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
  7. Terminology RDBMS MongoDB Table, View ➜ Collection Row ➜ Document

    Index ➜ Index Join ➜ Embedded Document Foreign Key ➜ Reference Partition ➜ Shard
  8. Entities in our Blogging System •  Users •  Articles • 

    Comments •  Tags •  Categories ( ? )
  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. Relational schema design •  Large ERD Diagrams •  Complex “create

    table” statements •  ORMs to map tables to objects •  Tables just to join tables together •  Lots of revisions until we get it right
  11. MongoDB ERD User ·Name ·Email address Article ·Name ·Slug ·Publish

    date ·Text ·Author Comment[] ·Comment ·Date ·Author Tag[] ·Value Category[] ·Value
  12. > var user = { username: ’thomas.r', first_name: ’Thomas', last_name:

    ’Rückstieß', } Start with an object (or array, hash, dict, etc)
  13. > db test > use blog switched to db blog

    // syntax is “db.<collection>.<command>” > db.users.insert( user ) Insert the Record No db/collection creation necessary
  14. // get one document (don’t care which one) > db.users.findOne()

    { "_id" : ObjectId("50804d0bd94ccab2da652599"), "username" : ”thomas.r", "first_name" : ”Thomas", "last_name" : ”Rückstieß" } Retrieve the Record again
  15. _id •  _id is the primary key in MongoDB • 

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

    Guaranteed to be unique across your cluster •  ObjectId("50804d0bd94ccab2da652599") ts mac pid inc
  17. > db.articles.insert( { title: ‘Hello World’, body: ‘This is my

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

    "body" : "This is my first blog post", "date" : ISODate("2013-06-20T00:00:00Z"), "username" : ”thomas.r", "tags" : [ "adventure", "mongodb" ], "comments" : [ ] } Finding the Article
  19. > db.articles.find( { _id : ObjectId("51c3bafafbd5d7261b4cdb5a") } ) .pretty() {

    "_id" : ObjectId("51c3bafafbd5d7261b4cdb5a"), "title" : "Hello World", "body" : "This is my first blog post", "date" : ISODate("2013-06-20T00:00:00Z"), "username" : ”thomas.r", "tags" : [ "adventure", "mongodb" ], "comments" : [ ] } Finding the Article
  20. > db.articles.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" : ”thomas.r", "tags" : [ "adventure", "mongodb" ], "comments" : [ ] } Querying An Array
  21. // the syntax is: “ update ( what, how )

    “ > db.articles.update( { _id: ObjectId("51c3bcddfbd5d7261b4cdb5b”) }, { "$push" : { "comments" : { "name" : "Steve Noname", "comment" : "Awesome Post" } } ) Using Update to Add a Comment
  22. > db.articles.find( { username: “thomas.r” } ).pretty() { "_id" :

    ObjectId("51c3bcddfbd5d7261b4cdb5b"), "body" : "This is my first blog post", "comments" : [ { "name" : "Steve Noname", "comment" : "Awesome Post" } ], "date" : ISODate("2013-06-20T00:00:00Z"), "tags" : [ "adventure", "mongodb" ], "title" : "Hello World", "username" : ”thomas.r" } Article with Comment Embedded
  23. // remove comment with $pull > var last_comment = {

    "name" : "Steve Noname", "comment" : "Awesome Post” } > db.articles.update( { _id: ObjectId("51c3bcddfbd5d7261b4cdb5b") }, { $pull : { comments: last_comment } } ) // remove article > db.articles.remove( { _id: ObjectId("51c3bcddfbd5d7261b4cdb5b") } ) Remove Comments / Articles
  24. MongoDB Drivers •  Official Support for 12 languages •  Community

    drivers for tons more •  Drivers connect to mongo servers •  Drivers translate BSON into native types •  mongo shell is not a driver, but works like one in some ways •  Installed using typical means (npm, pecl, gem, pip)
  25. Indexes in MongoDB 7 16 1 2 5 6 9

    12 18 21 B-Trees 13 {x: 13} Queries, inserts and deletes: O( log n ) time
  26. // create index with ensureIndex, client caches the // index

    build > db.articles.ensureIndex({ username: 1 }) > db.articles.ensureIndex({ date: –1 }) > db.articles.getIndexes() How Do I Create Indexes? [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.articles", "name" : "_id_" }, { "v" : 1, "key" : { "username" : 1 }, "ns" : "test.articles", "name" : "username_1" }, { "v" : 1, "key" : { "date" : -1 }, "ns" : "test.articles", "name" : "date_-1" } ]
  27. // Multiple fields (compound key indexes) > db.authors.ensureIndex({ first_name: 1,

    last_name: 1 }) // Arrays of values (multikey indexes) > var article = { title : "Hello World", body : "This is my first blog post", ... tags: [’adventure', ’mongodb'] } > db.recipes.ensureIndex({ tags: 1 }) What Can Be Indexed?
  28. // Sub-documents > var category = { name: ‘Technical Posts',

    maintainer: { username: ‘joe.example’, email_address: ’[email protected]’ } } db.categories.ensureIndex({ ’maintainer.username': 1 }) db.recipes.ensureIndex({ ’maintainer': 1 }) What Can Be Indexed?
  29. Index Options •  Uniqueness constraints (unique, dropDups) •  Sparse Indexes

    •  Geospatial Indexes •  TTL Collections (expireAfterSeconds)
  30. Profiling Slow Operations •  db.setProfilingLevel( n , slowms=100ms ) • 

    n=0 profiler off •  n=1 record operations longer than slowms •  n=2 record all queries •  db.system.profile.find( ) image: http://www.speareducation.com/
  31. > db.articles.find( { tags: ‘mongodb’ } ) .explain( ) {

    "cursor" : "BasicCursor" , "n" : 42, "nscannedObjects” : 13204, "nscanned" : 13204, ... "millis" : 356, ... } The Explain Plan (without Index)
  32. > db.articles.find( { tags: ‘mongodb’ } ) .explain( ) {

    "cursor" : "BtreeCursor tags_1”, ”isMultiKey" : true, "n" : 42, "nscannedObjects": 42 "nscanned" : 42, ”scanAndOrder" : false, "millis" : 0, ... } The Explain Plan (with Index)
  33. [email protected] @tomonezero Thomas Rückstieß #MongoDBDays Questions ? MongoDB Munich October

    14, Hilton Munich City www.mongodb.com/events/ mongodb-munich-2013