MongoDBor: How I Learned to Stop Worrying        and Love the JSON
• Humongous: Slang. Extraordinary large;  expressive coinage, perhaps reflecting huge  and monstrous, with stress pattern o...
ProductionDeployments
Format• BSON: Binary encoded serialization of  JSON documents• Characteristics • Lightweight: minimum overhead • Traversab...
JSON{    _id : Object(),    name : “mongoDB”,    date : Date(),    presenter: “isra”,    attendants : [                 {n...
Data schema• Database                     Database                       Collection• Collection             Document• Docu...
Collection• Flexible: no fixed structure • ALTER TABLE (implicit)• Created in the first insertion
Document• JSON document • _id • Object composed of fields • Arrays • Special objects: id, date, etc.
SQL to Mongo mapping
MongoDB basics• Default port: 27017• Optional authentication• Data location: /data/db/• Modes: automatic replication, auto...
Drivers• Officially supported • C, C++, Erlang, Haskell, Java,    Javascript, .NET, Perl, PHP, Python, Ruby,    Scala• Comm...
Connection• mongodb://username:password@host:port/  database?options • username and password are optional • port: 27017 by...
Basics• show dbs• use db_name• show collections (current db)• show users (current db)
InsertionColección de las charlas del GUL:db.gul.insert ({  name : “mongoDB”,  date : new Date(),  presenter: “isra”,  att...
Querying• Charla de MongoDB:  db.gul.find({name:”mongoDB”});• Charlas a las que asistió ana:  db.gul.find({attendants.name:”...
Querying II• Charlas ordenadas por nombre:  db.gul.find().sort({name:1});• Las 5 primeras charlas:  db.gul.find().limit(5);•...
Querying III• Charlas con algún asistente menor de  edad: db.gul.find({attendants.age:{$lt:  18}});• Las charlas anteriores...
Querying IV• Charlas asistidas por pedro y/o ana:  db.gul.find({“attendants.name”:  {$in:[“pedro”, “ana”]}  });• Charlas as...
Querying V (group)• Número de charlas: db.gul.count();• Número de charlas impartidas por isra:  db.gul.count({presenter:”i...
Querying VI (group)• Número de charlas con los mismos  asistentes  db.gul.group({      key: {attendants:1},      initial: ...
Querying VII (group)• Número de charlas con los mismos  asistentes, desde el martes  db.gul.group({      key: {attendants:...
Querying VIII (group)• Número de charlas con los mismos  asistentes, y media de edades  db.gul.group({      ...  finalize: ...
Querying IX (group)• Número de charlas por día de la semana  db.gul.group({      ...  keyf: function(doc){      return {we...
Update• db.gul.save(x) //updates if exits; inserts if  new• Actualizar ponentes de la charla de Crafty  db.gul.update({nam...
Update II• findAndModify
Remove• db.gul.remove() //removes all• db.gul.remove({presenter:”isra”})
Database references• Simple direct linking  isra = db.gul_members.findOne()  {_id: ObjectId(“ad234fea23482348”),  name:”isr...
Database references• DBRef  isra = db.gul_members.findOne()  {_id: ObjectId(“ad234fea23482348”),  name:”isra”, age:31, lang...
Map/Reduce• Batch processing of data and aggregation  operations• Where GROUP BY was used in SQL• Input from a collection ...
Map/reduce (II)• Número de cursos asistidos por persona    var map = function(){       for(var i in this.attendants){     ...
Map/reduce (III)• Número de cursos asistidos por persona    var reduce = function(key, values){      var sum=0;      for (...
Map/reduce (IV)• Número de cursos asistidos por persona  db.gul.mapReduce({      map: map,      reduce: reduce,      {out:...
Schema Design• Function of the data and the use case• Decisions • # of collections • Embedding or linking • Indexes • Shar...
Indexes• Data structure• Query optimization• B-Tree indexes• db.gul.ensureIndex({ name:1 });
Sharding• Scalability• Horizontal partitioning of a database• A BSON document stored in ONE shard• Shard key• Mongo offers...
Sharding• MongoDB offers sharding • Auto balancing • Easy addition of new machines • Up to 1k nodes • No single point of f...
References• MongoDB devel docs: http://  www.mongodb.org/display/DOCS/  Developer+Zone• MongoDB FAQ: http://www.mongodb.or...
References• Kyle Banker’s blog: • Aggregation: http://kylebanker.com/blog/    2009/11/mongodb-count-group/ • e-Commerce ex...
Thank you very much!   Any questions?
Upcoming SlideShare
Loading in …5
×

MongoDB at GUL

632 views

Published on

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

  • Be the first to like this

No Downloads
Views
Total views
632
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • MongoDB at GUL

    1. 1. MongoDBor: How I Learned to Stop Worrying and Love the JSON
    2. 2. • Humongous: Slang. Extraordinary large; expressive coinage, perhaps reflecting huge and monstrous, with stress pattern of tremendous• Open source NoSQL database • Written in C++ • https://github.com/mongodb/mongo
    3. 3. ProductionDeployments
    4. 4. Format• BSON: Binary encoded serialization of JSON documents• Characteristics • Lightweight: minimum overhead • Traversable • Efficient: encoding and decoding
    5. 5. JSON{ _id : Object(), name : “mongoDB”, date : Date(), presenter: “isra”, attendants : [ {name:”ana”, age:23}, {name:”luis”, age: 32} ]}
    6. 6. Data schema• Database Database Collection• Collection Document• Documents (JSON) { user: 1, name: [] }
    7. 7. Collection• Flexible: no fixed structure • ALTER TABLE (implicit)• Created in the first insertion
    8. 8. Document• JSON document • _id • Object composed of fields • Arrays • Special objects: id, date, etc.
    9. 9. SQL to Mongo mapping
    10. 10. MongoDB basics• Default port: 27017• Optional authentication• Data location: /data/db/• Modes: automatic replication, automatic fail- over
    11. 11. Drivers• Officially supported • C, C++, Erlang, Haskell, Java, Javascript, .NET, Perl, PHP, Python, Ruby, Scala• Community supported • ActionScript, C#, Delphi, etc.
    12. 12. Connection• mongodb://username:password@host:port/ database?options • username and password are optional • port: 27017 by default • database: admin database by default • options: name=value pairs
    13. 13. Basics• show dbs• use db_name• show collections (current db)• show users (current db)
    14. 14. InsertionColección de las charlas del GUL:db.gul.insert ({ name : “mongoDB”, date : new Date(), presenter: “isra”, attendants : [ {name:”ana”, age:23}, {name:”luis”, age: 32} ]})
    15. 15. Querying• Charla de MongoDB: db.gul.find({name:”mongoDB”});• Charlas a las que asistió ana: db.gul.find({attendants.name:”ana”});• Nombre de las charlas impartidas por isra: db.gul.find({presenter:”isra”}, {name:1});
    16. 16. Querying II• Charlas ordenadas por nombre: db.gul.find().sort({name:1});• Las 5 primeras charlas: db.gul.find().limit(5);• Las 5 siguientes: db.gul.find().skip(5).limit(5);• La primera charla: db.gul.findOne()
    17. 17. Querying III• Charlas con algún asistente menor de edad: db.gul.find({attendants.age:{$lt: 18}});• Las charlas anteriores entre el lunes y jueves: db.gul.find({date: {$gt:new Date(2012,02,11), $lt:new Date(2012,02,14)} });
    18. 18. Querying IV• Charlas asistidas por pedro y/o ana: db.gul.find({“attendants.name”: {$in:[“pedro”, “ana”]} });• Charlas asistidas por 10 personas: db.gul.find({attendants:{$size:10}});
    19. 19. Querying V (group)• Número de charlas: db.gul.count();• Número de charlas impartidas por isra: db.gul.count({presenter:”isra”});• Distintos asistentes a todas las charlas: db.gul.distinct(“attendants.name”);
    20. 20. Querying VI (group)• Número de charlas con los mismos asistentes db.gul.group({ key: {attendants:1}, initial: {sum:0}, reduce: function(doc,prev){ prev.sum+=1; } });
    21. 21. Querying VII (group)• Número de charlas con los mismos asistentes, desde el martes db.gul.group({ key: {attendants:1}, initial: {sum:0}, reduce: function(doc,prev){ prev.sum+=1;}, cond: {date: {“$gte”:new Date(2012, 2,12)}} });
    22. 22. Querying VIII (group)• Número de charlas con los mismos asistentes, y media de edades db.gul.group({ ... finalize: function(doc){ var sum=0; for(var i in doc.attendants){ sum+=doc.attendants[i].age;} doc.av_age=sum/ doc.attendants.length; } });
    23. 23. Querying IX (group)• Número de charlas por día de la semana db.gul.group({ ... keyf: function(doc){ return {week_day: doc.date.getDay()}; } });
    24. 24. Update• db.gul.save(x) //updates if exits; inserts if new• Actualizar ponentes de la charla de Crafty db.gul.update({name:”Crafty”}, {$set: {presenter:[“javi”,“isra”]}});• Nuevo asistente a la charla db.gul.update({name:”mongoDB”},{$push: {name:”pepe”, age:19}});
    25. 25. Update II• findAndModify
    26. 26. Remove• db.gul.remove() //removes all• db.gul.remove({presenter:”isra”})
    27. 27. Database references• Simple direct linking isra = db.gul_members.findOne() {_id: ObjectId(“ad234fea23482348”), name:”isra”, age:31, languages:”js”} db.gul.find({“attendants.name”:isra._id})
    28. 28. Database references• DBRef isra = db.gul_members.findOne() {_id: ObjectId(“ad234fea23482348”), name:”isra”, age:31, languages:”js”} db.gul.insert({name: “mongoDB”, presenter:new DBRef(“gul_members”,isra._id ))
    29. 29. Map/Reduce• Batch processing of data and aggregation operations• Where GROUP BY was used in SQL• Input from a collection and output going to a collection
    30. 30. Map/reduce (II)• Número de cursos asistidos por persona var map = function(){ for(var i in this.attendants){ emit(this.attendants[i].name,1); } }
    31. 31. Map/reduce (III)• Número de cursos asistidos por persona var reduce = function(key, values){ var sum=0; for (var i in values){ sum+=values[i]; } return sum; }
    32. 32. Map/reduce (IV)• Número de cursos asistidos por persona db.gul.mapReduce({ map: map, reduce: reduce, {out: “my_new_collection”} });
    33. 33. Schema Design• Function of the data and the use case• Decisions • # of collections • Embedding or linking • Indexes • Sharding
    34. 34. Indexes• Data structure• Query optimization• B-Tree indexes• db.gul.ensureIndex({ name:1 });
    35. 35. Sharding• Scalability• Horizontal partitioning of a database• A BSON document stored in ONE shard• Shard key• Mongo offers auto-sharding
    36. 36. Sharding• MongoDB offers sharding • Auto balancing • Easy addition of new machines • Up to 1k nodes • No single point of failure • Automatic failover
    37. 37. References• MongoDB devel docs: http:// www.mongodb.org/display/DOCS/ Developer+Zone• MongoDB FAQ: http://www.mongodb.org/ display/DOCS/Developer+FAQ• MongoDB cookbook: http:// cookbook.mongodb.org/
    38. 38. References• Kyle Banker’s blog: • Aggregation: http://kylebanker.com/blog/ 2009/11/mongodb-count-group/ • e-Commerce example: http:// kylebanker.com/blog/2010/04/30/ mongodb-and-ecommerce/
    39. 39. Thank you very much! Any questions?

    ×