MongoDb In Action

14,087 views

Published on

how to use mongodb as MySQL

Published in: Technology, Business
2 Comments
37 Likes
Statistics
Notes
No Downloads
Views
Total views
14,087
On SlideShare
0
From Embeds
0
Number of Embeds
4,685
Actions
Shares
0
Downloads
854
Comments
2
Likes
37
Embeds 0
No embeds

No notes for slide

MongoDb In Action

  1. 1. MongoDB In Action<br />超群.com<br />@fuchaoqun<br />http://www.fuchaoqun.com<br />
  2. 2. I will talk ….<br /><ul><li>About MongoDB
  3. 3. Dynamic querys
  4. 4. Replication
  5. 5. Sharding
  6. 6. GridFS
  7. 7. Performance</li></li></ul><li>
  8. 8. About MongoDB?<br /><ul><li>Document-oriented
  9. 9. JSON-style documents
  10. 10. Schema-free
  11. 11. Written in C++ for high performance
  12. 12. Scalable
  13. 13. MapReduce
  14. 14. Many supported platforms & languages</li></li></ul><li>Dynamic querysSelect<br />MySQL:<br />SELECT * FROM user<br />Mongo: <br />db.user.find()<br />MySQL:<br />SELECT * FROM user WHERE name = &apos;foobar&apos;<br />Mongo: <br />db.user.find({&apos;name&apos; : &apos;foobar&apos;})<br />
  15. 15. Dynamic querysInsert<br />MySQL:<br />INSERT INOT user (`name`, `age`) values (&apos;foobar&apos;,25)<br />Mongo: <br />db.user.insert({&apos;name&apos; : &apos;foobar&apos;, &apos;age&apos; : 25})<br />if you want add a column `email` on MySQL,you must :<br />ALTER TABLE user….<br />But in Mongo,you can just: <br />db.user.insert({&apos;name&apos; : &apos;foobar&apos;, &apos;age&apos; : 25, &apos;email&apos; : &apos;foo@bar.com&apos;})<br />
  16. 16. Dynamic querysDelete<br />MySQL:<br />DELETE * FROM user<br />Mongo: <br />db.user.remove({})<br />MySQL:<br />DELETE FROM user WHERE age &lt; 30<br />Mongo: <br />db.user.remove({&apos;age&apos; : {$lt : 30}})<br />$gt : &gt; ; $gte : &gt;= ; $lt : &lt; ; $lte : &lt;= ; $ne : !=<br />
  17. 17. Dynamic querysUpdate<br />MySQL:<br />UPDATE user SET `age` = 36 WHERE `name` = &apos;foobar&apos;<br />Mongo: <br />db.user.update({&apos;name&apos; : &apos;foobar&apos;}, {$set : {&apos;age&apos; : 36}})<br />MySQL:<br /> UPDATE user SET `age` = `age` + 3 WHERE `name` = &apos;foobar&apos;<br />Mongo: <br />db.user.update({&apos;name&apos; : &apos;foobar&apos;}, {$inc : {&apos;age&apos; : 3}})<br />See more @ http://tinyurl.com/yka6ras<br />
  18. 18. Dynamic querysAdvanced<br />MySQL:<br />SELECT COUNT(*) FROM user WHERE `name` = &apos;foobar&apos;<br />Mongo: <br />db.user.find({&apos;name&apos; : &apos;foobar&apos;}).count()<br />MySQL:<br /> SELECT * FROM user limit 10,20<br />Mongo: <br />db.user.find().skip(10).limit(20)<br />
  19. 19. Dynamic querysAdvanced<br />MySQL:<br />SELECT * FROM user WHERE `age` IN (25, 35,45)<br />Mongo: <br />db.user.find({&apos;age&apos; : {$in : [25, 35, 45]}})<br />MySQL:<br /> SELECT * FROM user limit 10,20<br />Mongo: <br />db.user.find().skip(10).limit(20)<br />
  20. 20. Dynamic querysAdvanced<br />MySQL:<br />SELECT * FROM user ORDER BY age DESC<br />Mongo: <br />db.user.find().sort({&apos;age&apos; : -1})<br />MySQL:<br /> SELECT DISTINCT(name) FROM user WHERE age &gt; 20<br />Mongo: <br />db.user.distinct(&apos;name&apos;, {&apos;age&apos;: {$lt : 20}})<br />
  21. 21. Dynamic querysAdvanced<br />MySQL:<br />SELECT name, sum(marks) FROM user GROUP BY name<br />Mongo: <br />db.user.group({<br /> key : {&apos;name&apos; : true},<br />cond: {&apos;name&apos; : &apos;foo&apos;},<br /> reduce: function(obj,prev) { prev.msum += obj.marks; },<br /> initial: {msum : 0}<br /> });<br />MySQL:<br /> SELECT name FROM user WHERE age &lt; 20<br />Mongo: <br />db.user.find(&apos;this.age &lt; 20&apos;, {name : 1})<br />See more @ http://tinyurl.com/ykyuuwo<br />
  22. 22. So….<br />MongoDB can do almost everything that MySQL can do, and quicker & smarter<br />
  23. 23. Replication<br /><ul><li>Master-Slave Replication
  24. 24. Replica Pairs
  25. 25. Limited Master-Master Replication</li></li></ul><li>Sharding<br />
  26. 26. GridFS<br /><ul><li>files:object metadata
  27. 27. chunks:binary data</li></li></ul><li>Use GridFS In PHP<br />
  28. 28. GridFS Projects<br /><ul><li>nginx-gridfshttp://github.com/mdirolf/nginx-gridfs
  29. 29. gridfs-fusehttp://github.com/mikejs/gridfs-fuse</li></li></ul><li>Performance<br />Benchmark means nothing , but most of us like it.<br />From: http://tinyurl.com/okq7m5<br />From: http://tinyurl.com/yc3rlld<br />See more @ http://tinyurl.com/yanwn2b<br />
  30. 30. Who is using MongoDB?<br />See more @ http://tinyurl.com/dhrryw<br />
  31. 31. Thanks!<br />Q&A<br />

×