Introduction to MongoDB

2,479 views
2,320 views

Published on

An introduction to MongoDB: Why should you choose, and how will you migrate to it? What you should consider and What to learn? Syntax, highlights and more

Published in: Technology, Design
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,479
On SlideShare
0
From Embeds
0
Number of Embeds
1,329
Actions
Shares
0
Downloads
1
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Introduction to MongoDB

  1. 1. Scale Hacking Introduction to MongoDB moshe.kaplan@brightaqua.com http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com
  2. 2. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com About 2 Scale
  3. 3. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com HELLO. MY NAME IS MONGODB Introduction 3
  4. 4. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com Who is Using mongoDB? 4
  5. 5. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com Who is Behind mongoDB
  6. 6. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com Key Value Store (with benefits) insert get multiget remove truncate 6 <Key, Value> http://wiki.apache.org/cassandra/API
  7. 7. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com When Should I Choose NoSQL? Eventually Consistent Document Store Key Value 7 http://guyharrison.squarespace.com/blog/tag/nosql
  8. 8. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com What mongoDB is Made of? 8 http://www.10gen.com/products/mongodb
  9. 9. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com Why MongoDB? What? Why? JSON End to End No Schema “No DBA”, Just Serialize Write 10K Inserts/sec on virtual machine Read Similar to MySQL HA 10 min to setup a cluster Sharding Out of the Box LBS Great for that No Schema None: no downtime to create new columns Buzz Trend is with NoSQL 9
  10. 10. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com DESIGN FOR NOSQL Introduction 10
  11. 11. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com Database for Software Engineers Class Subclass Document Subdocument
  12. 12. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com Same Terminology Database  Database Table  Collection Row  Document
  13. 13. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com A Blog Case Study in MySQL http://www.slideshare.net/nateabele/building-apps-with-mongodb
  14. 14. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com as a SW Engineer would like it to be… http://www.slideshare.net/nateabele/building-apps-with-mongodb
  15. 15. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com INSTALLATION Introduction 15
  16. 16. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com A free hosting environment for you
  17. 17. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com The CentOS/RHEL Way Add to /etc/yum.repos.d/10gen.repo [10gen] name=10gen Repository baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64 gpgcheck=0 enabled=1 yum –y install mongo-10gen mongo-10gen-server The Packages: mongo-10gen: tools mongo-10gen-server: mongod and mongos http://docs.mongodb.org/manual/tutorial/install-mongodb-on-red-hat-centos-or-fedora-linux/
  18. 18. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com USAGE BASICS Introduction 18
  19. 19. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com Connect to the Database Connect: • > mongo Show current database: • >> db Show Databases • >> show databases; Show Collections • >> show collections;
  20. 20. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com Databases Manipulation: Create & Drop Change Database: >> use <database> Create Database Just switch and create an object… Delete Database > use mydb; > db.dropDatabase();
  21. 21. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com Collections Manipulation Create Collcation • > db.createCollection(collectionName) Delete Collection • > db.collectionName.drop() Or just insert to it
  22. 22. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com INSERT j = { name : "mongo" } k = { x : 3 } db.things.insert( j ) db.things.insert( k )
  23. 23. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com SELECT: No SQL, just ORM… Select All db.things.find() WHERE db.posts.find({“comments.email” : ”b@c.com”}) Pattern Matching db.posts.find( {“title” : /mongo/i} ) Sort db.posts.find().sort({email : 1, date : -1}); Limit db.posts.find().limit(3)
  24. 24. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com Specific fields Select All db.users.find( { }, { user_id: 1, status: 1, _id: 0 } ) 1: Show; 0: don’t show < WHERE < SELECT user_id, status
  25. 25. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com WHERE != “A” { $ne: "A" } > 25 { $gt: 25 } > 25 AND <= 50 { $gt: 25, $lte: 50 } Like ‘bc%’ /^bc/ < 25 OR >= 50 { $or : [ { $lt: 25 }, { $gte : 50 } ] }
  26. 26. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com Join Wrong Place… Or Map Reduce
  27. 27. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com GROUP BY db.article.aggregate( { $group : { _id : { author : "$author“, name : “$name” }, docsPerAuthor : { $sum : 1 }, viewsPerAuthor : { $sum : "$pageViews" } }} ); < GROUP BY author, name < SUM(pageViews) < SUM(1) = N
  28. 28. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com UPDATE db.posts.update( {“comments.email”: ”b@c.com”}, {$set : {“comments.email”: ”d@c.com”}} } SET age = age + 3 db.users.update( { status: "A" } , { $inc: { age: 3 } }, { multi: true } )
  29. 29. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com DELETE db.users.remove( { status: "D" } )
  30. 30. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com THE MEAN PLATFORM Introduction 30
  31. 31. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com31
  32. 32. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com The MEAN flow 32 http://scotch.io/tutorials/javascript/creating-a-single-page-todo-app-with-node-and-angular
  33. 33. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com Node + Express Installation • yum repolist • sudo rpm --import https://fedoraproject.org/static/0608B895.txt • sudo rpm -Uvh http://download- i2.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm • sudo yum -y install nodejs npm --enablerepo=epel • sudo yum -y install npm --enablerepo=epel • sudo npm install -g express • sudo npm install -g express-generator 33
  34. 34. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com My First App • express node_test • sudo vi package.json • , • "mongodb": "*", • "monk": "*“ • cp node_test • npm install • mkdir data • npm start • wget http://localhost:3000/ 34
  35. 35. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com THE A/A CASE STUDY Introduction 35
  36. 36. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com DNS based Load Balancing Architecture 36
  37. 37. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com MIGRATION CONSIDERATIONS Introduction 37
  38. 38. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com Data Migration Map the table structure Export the data and Import It Add Indexes 38 http://igcse-geography-lancaster.wikispaces.com/1.2+MIGRATION
  39. 39. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com Selected Migration Tool 39
  40. 40. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com Usage Details > Install ruby > gem install mongify … Modify the code to your needs … Create configuration files > mongify translation db.config > translation.rb > mongify process db.config translation.rb 40
  41. 41. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com Date Functions Year(), Month()… function included … buy only in the JavaScript engine Solution: New fields! [original field] [original field]_[year part] [original field]_[month part] [original field]_[day part] [original field]_[hour part] 41
  42. 42. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com NO SCHEMA IS A GOOD THING BUT… Schemaless 42
  43. 43. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com Default Values No Schema No Default Values App Challenge Timestamps… No single source of truth 43
  44. 44. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com Casting and Type Safety No Schema No … App Challenge 44
  45. 45. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com Auto Numbers Start using _id { "_id" : 0, "health" : 1, "stateStr" : "PRIMARY", "uptime" : 59917, } Counter tables Dedicated database 1:1 Mapping Counter++ using findAndModify 45
  46. 46. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com The ORM Solution 46
  47. 47. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com Data Analysts 47 http://www.designersplayground.com/pr/internet-meme-list/data-analyst-2/
  48. 48. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com Data Analysts This is not SQL There are no joins No perfect tools 48 Pentaho RockMongoMongoVUE RoboMongo
  49. 49. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com No Joins Do in the application Leverage the power of NoSQL 49 http://www.slideshare.net/nateabele/building-apps-with-mongodb
  50. 50. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com Limited Resultset 50 16MB document size Limit and Skip Adjusted WHERE
  51. 51. http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com Bottom Line Powerful tool Embrace the Challenge Schema-less limitations: counters, data types Tools for Data Scientists Data design 51
  52. 52. Scale Hacking Moshe Kaplan 972-54-2291978 moshe.kaplan@brightaqua.com http://blogs.microsoft.co.il/blogs/vprnd http://top-performance.blogspot.com

×