Building your first app with mongo db

2,377 views
2,138 views

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,377
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
49
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Building your first app with mongo db

  1. 1. #MongoDBDays Building your first app; an introduction to MongoDB Thomas Rückstieß Technical Services Engineer, MongoDB
  2. 2. Notes to the Presenter •  Themes for this presentation: •  First presentation in the conference. Keep it fun •  User is likely brand new to MongoDB and comes from traditional relational background •  This is an introduction. Don't go deep.. Introduce concepts and refer to later presentations that build on these concepts.
  3. 3. What is MongoDB?
  4. 4. MongoDB is a ___________ database •  Document •  Open source •  High performance •  Horizontally scalable •  Full featured
  5. 5. 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" } }
  6. 6. 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
  7. 7. 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
  8. 8. Horizontally Scalable Shard 1 Shard 2 Shard 3 Horizontally Scalable Shard N
  9. 9. Full Featured •  Ad Hoc queries •  Real time aggregation •  Rich query capabilities •  Strongly consistent •  Geospatial features •  Support for most programming languages •  Flexible schema
  10. 10. Scalability & Performance Scalability & Performance Database Landscape Memcached MongoDB RDBMS Depth of Functionality
  11. 11. mongodb.org/downloads
  12. 12. Running MongoDB $ tar –z xvf mongodb-osx-x86_64-2.4.x.tgz $ cd mongodb-osx-i386-2.4.4/bin $ mkdir –p /data/db $ ./mongod
  13. 13. Mongo Shell $ 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" }
  14. 14. Document Database
  15. 15. Terminology RDBMS MongoDB Table, View ➜ Collection Row ➜ Document Index ➜ Index Join ➜ Embedded Document Foreign Key ➜ Reference Partition ➜ Shard
  16. 16. Let’s Build a Blog
  17. 17. First step in any application is Determine your entities
  18. 18. Entities in our Blogging System •  Users •  Articles •  Comments •  Tags •  Categories ( ? )
  19. 19. In a relational database app We would start by doing schema design
  20. 20. Typical (relational) ERD Category ·Name ·URL User ·Name ·Email address Article ·Name ·Slug ·Publish date ·Text Comment ·Comment ·Date ·Author Tag ·Name ·URL
  21. 21. 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
  22. 22. In a MongoDB based app We start building our app and let the schema evolve
  23. 23. MongoDB ERD Article User ·Name ·Email address ·Name ·Slug ·Publish date ·Text ·Author Comment[] ·Comment ·Date ·Author Tag[] ·Value Category[] ·Value
  24. 24. Working With MongoDB
  25. 25. The Shell
  26. 26. Start with an object (or array, hash, dict, etc) > var user = { username: ’thomas.r', first_name: ’Thomas', last_name: ’Rückstieß', }
  27. 27. Insert the Record > db test > use blog switched to db blog // syntax is “db.<collection>.<command>” > db.users.insert( user ) No db/collection creation necessary
  28. 28. Retrieve the Record again // get one document (don’t care which one) > db.users.findOne() { "_id" : ObjectId("50804d0bd94ccab2da652599"), "username" : ”thomas.r", "first_name" : ”Thomas", "last_name" : ”Rückstieß" }
  29. 29. _id •  _id is the primary key in MongoDB •  Any unique immutable value could be used •  Automatically created as an ObjectId if not provided •  Automatically indexed
  30. 30. ObjectId •  ObjectId is a special 12 byte value •  Guaranteed to be unique across your cluster •  ObjectId("50804d0bd94ccab2da652599") ts mac pid inc
  31. 31. Creating a Blog Article > 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: [ ] })
  32. 32. Finding the Article > 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" : [ ] }
  33. 33. Finding the Article > 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" : [ ] }
  34. 34. Querying An Array > 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" : [ ] }
  35. 35. Using Update to Add a Comment // the syntax is: “ update ( what, how ) “ > db.articles.update( { _id: ObjectId("51c3bcddfbd5d7261b4cdb5b”) }, { "$push" : { "comments" : { "name" : "Steve Noname", "comment" : "Awesome Post" } } )
  36. 36. Article with Comment Embedded > 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" }
  37. 37. Remove Comments / Articles // 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") } )
  38. 38. MongoDB Drivers
  39. 39. Real applications are not built in the shell
  40. 40. MongoDB has native bindings for over 12 languages
  41. 41. 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)
  42. 42. Learn more about MongoDB
  43. 43. Manual: docs.mongodb.org
  44. 44. Online Training at MongoDB University
  45. 45. We've introduced a lot of concepts here
  46. 46. Schema Design @ 11:00 Article User ·Name ·Email address ·Name ·Slug ·Publish date ·Text ·Author Comment[] ·Comment ·Date ·Author Tag[] ·Value Category[] ·Value
  47. 47. Replication @ 11:50 Client Application Driver Re a d a Re d Write Secondary Primary Secondary
  48. 48. Sharding @ 14:00 www.etiennemansard.com
  49. 49. Indexing @ 15:50 7 1 2 5 6 16 9 12 18 21
  50. 50. #MongoDBDays Questions ? Thomas Rückstieß thomas@mongodb.com @tomonezero

×