Ce diaporama a bien été signalé.
Le téléchargement de votre SlideShare est en cours. ×

Building your first app with MongoDB

Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Chargement dans…3
×

Consultez-les par la suite

1 sur 73 Publicité

Plus De Contenu Connexe

Diaporamas pour vous (20)

Les utilisateurs ont également aimé (20)

Publicité

Similaire à Building your first app with MongoDB (20)

Plus par Norberto Leite (20)

Publicité

Plus récents (20)

Building your first app with MongoDB

  1. 1. #mongodbdays @mongodb @nleite #developers Building your first app An introduction to MongoDB Norberto Leite SA, MongoDB
  2. 2. First Things First!
  3. 3. Let’s not talk about Fußball!
  4. 4. Quick Introduction • Norberto Leite – SA – Madrid, Spain – norberto@mongodb.com – @nleite
  5. 5. Welcome to MongoDB Days Munich! This is YOUR conference!
  6. 6. Thanks for being part of the Family!
  7. 7. Grab our staff for anything you need!
  8. 8. What is MongoDB?
  9. 9. MongoDB is a ___________ database • Document • Open source • High performance • Horizontally scalable • Full featured
  10. 10. 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
  11. 11. Open Source • MongoDB is an open source project • On GitHub • Licensed under the AGPL • Started & sponsored by MongoDB Inc (formerly 10gen) • Commercial licenses available • Contributions welcome
  12. 12. 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
  13. 13. Shard 1 Shard 2 Shard 3 Shard N Horizontally Scalable
  14. 14. Database Landscape
  15. 15. Full Featured • Ad Hoc queries • Real time aggregation • Rich query capabilities • Strongly consistent • Geospatial features • Support for most programming languages • Flexible schema
  16. 16. Setting Expectations • What is MongoDB • How to develop with MongoDB • Scale with MongoDB • Analytics • MMS • Sharding • Setting the correct environment
  17. 17. Ready to become a Pro!
  18. 18. mongodb.org/downloads
  19. 19. Running MongoDB $ tar –z xvf mongodb-osx-x86_64-2.6.5.tgz $ cd mongodb-osx-i386-2.4.4/bin $ mkdir –p /data/db $ ./mongod
  20. 20. Mongo Shell MacBook-Air-:~ $ mongo MongoDB shell version: 2.6.5 connecting to: test > db.test.insert({text: 'Welcome to MongoDB'}) > db.test.find().pretty() { "_id" : ObjectId("51c34130fbd5d7261b4cdb55"), "text" : "Welcome to MongoDB" }
  21. 21. Document Database
  22. 22. Terminology RDBMS MongoDB Table, View ➜ Collection Row ➜ Document Index ➜ Index Join ➜ Embedded Document Foreign Key ➜ Reference Partition ➜ Shard
  23. 23. Let’s Build a Blog
  24. 24. Let’s Build a Blog
  25. 25. Let’s Build a Personal Data Hub!
  26. 26. First step in any application is Determine your entities
  27. 27. Entities in our Data Hub • Accounts • Messages – emails – tweets – comments – streams • Notifications
  28. 28. In a relational base app We would start by doing schema design
  29. 29. Typical (relational) ERD Messages Email Tweets Facebook messages Alerts Accounts
  30. 30. Das ist beängstigende Sache
  31. 31. In a MongoDB based app We start building our app and let the schema evolve
  32. 32. MongoDB ERD Accounts - account - user - password - refresh_rate - uri Alerts Messages - id - time - account_id - from - to - body - attachments - text - user - time - retweets - subscribers - channel - rate - period - metrics:[] …
  33. 33. Working With MongoDB
  34. 34. Demo time
  35. 35. Switch to Your DB >db test > use datahub switching to db datahub
  36. 36. Create our first Document >var account = { "name": "gmail", "credentials": { "user": "norberto@mongodb.com", "password": "YOU WISH!" }, "smtp": "smpt.gmail.com", "tls": true }
  37. 37. Switch to Your DB >db test > use datahub switching to db datahub > db.accounts.insert( account )
  38. 38. Insert the Record > db.accounts.insert(account) No collection creation necessary
  39. 39. Find One Record > db.accounts.findOne() { "_id": ObjectId("54490561150027cc775b1019"), "name": "gmail", "credentials": { "user": "norberto@mongodb.com", "password": "YOU WISH!" }, "smtp": "smpt.gmail.com", "tls": true }
  40. 40. _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
  41. 41. 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
  42. 42. Rich Data Types > db.accounts.findOne() { "_id": ObjectId("54490561150027cc775b1019"), "name": "gmail", "credentials": { Strings "user": "norberto@mongodb.com", "password": "YOU WISH!" }, ”last_access": ISODate("2014-10-30T13:09:36.724Z"), "smtp": "smpt.gmail.com", "tls": true } Date Boolean
  43. 43. BSON
  44. 44. Inserting Messages (emails, tweets …) > db.messages.insert({ "_id" : ObjectId("54527e08257844421e64623f"), "favorited" : false, "contributors" : null, "truncated" : false, "text" : "converting to #java 8", "in_reply_to_status_id" : null, ”hashtags”: [ "#java", ] … }
  45. 45. Inserting Messages (emails, tweets …) > db.messages.insert({ "_id" : ObjectId("54523d2d25784427c6fabce1"), "From" : "norberto@mongodb.com", "To" : "mongodb-user@googlegroups.com", "Date" : ISODate("2012-08-15T22:32:34Z"), "body" : { "text/plain" : ”Hello Munich, nice to see yalll!" }, "Subject" : ”Live From MongoDB World" })
  46. 46. Finding a Message > db.message.find().pretty() { "_id" : ObjectId("54523d2d25784427c6fabce1"), "From" : "norberto@mongodb.com", "To" : "mongodb-user@googlegroups.com", "Date" : ISODate("2012-08-15T22:32:34Z"), "body" : { "text/plain" : ”Hello Munich, nice to see yalll!" }, "Subject" : ”Live From MongoDB World" } { "_id" : ObjectId("54527e08257844421e64623f"), "favorited" : false, "contributors" : null, "truncated" : false, "text" : "converting to #java 8", "in_reply_to_status_id" : null, ”hashtags”: [ "#java", ] … }
  47. 47. Querying An Array > db.article.find({"hashtags":"#java"}).pretty() { "_id" : ObjectId("54527e08257844421e64623f"), "favorited" : false, "contributors" : null, "truncated" : false, "text" : "converting to #java 8, #programing ", "in_reply_to_status_id" : null, ”hashtags”: [ "#java", "#programing"] … } query in JSON
  48. 48. Using Update to Add a Comment > db.messages.update({ "_id" : ObjectId("54523d2d25784427c6fabce1") }, {$set: { opened: {date: ISODate("2012-08-15T22:32:34Z"), user: ’Norberto'} } }) > which is a subdocument set new field on the document
  49. 49. Post with Comment Attached > db.message.findOne({"_id" : ObjectId("54523d2d25784427c6fabce1")}) { "_id" : ObjectId("54523d2d25784427c6fabce1"), "From" : "norberto@mongodb.com", "To" : "mongodb-user@googlegroups.com", "Date" : ISODate("2012-08-15T22:32:34Z"), "body" : { "text/plain" : ”Hello Munich, nice to see yalll!" }, "Subject" : ”Live From MongoDB World” "opened" : {"date": ISODate("2012-08-15T22:32:34Z"), "user": ’Norberto'} } Find document by primary key
  50. 50. MongoDB Drivers
  51. 51. Real applications are not built in the shell
  52. 52. MongoDB has native bindings for over 12 languages
  53. 53. Morphia MEAN Stack Java Python Perl Ruby Support for the most popular languages and frameworks
  54. 54. Great, I’m excited! What’s next?
  55. 55. docs.mongodb.org
  56. 56. Never Stop Learning!
  57. 57. Schema Design, Schema Design, Schema Design, Schema Design!
  58. 58. Legacy Migration 1. Copy existing schema & some data to MongoDB 2. Iterate schema design development Measure performance, find bottlenecks, and embed 1. one to one associations first 2. one to many associations next 3. many to many associations 3. Migrate full dataset to new schema New Software Application? Embed by default
  59. 59. Embedding over Referencing • Embedding is a bit like pre-joined data – BSON (Binary JSON) document ops are easy for the server • Embed (90/10 following rule of thumb) – When the “one” or “many” objects are viewed in the context of their parent – For performance – For atomicity • Reference – When you need more scaling – For easy consistency with “many to many” associations without duplicated data
  60. 60. It’s All About Your Application • Programs+Databases = (Big) Data Applications • Your schema is the impedance matcher – Design choices: normalize/denormalize, reference/embed – Melds programming with MongoDB for best of both – Flexible for development and change • Programs×MongoDB = Great Big Data Applications
  61. 61. We've introduced a lot of concepts here
  62. 62. IoT
  63. 63. MMS @ Scale Easily Best Practices, Automated Cut Management Overhead
  64. 64. Scalability @
  65. 65. DevOps @ Provision Upgrade Scale Continuous Backup Point-in-Time Recovery Performance Alerts
  66. 66. Wrapping up … • MongoDB is a great Developers Tool • Designed for : • Scalability • Flexibility • Performance • Multipurpose • Great Ecosystem
  67. 67. Well Done !
  68. 68. Questions?
  69. 69. #mongodbdays Obrigado! Norberto Leite norberto@mongodb.com

×