MongoDB at GUL
Upcoming SlideShare
Loading in...5
×
 

MongoDB at GUL

on

  • 588 views

 

Statistics

Views

Total Views
588
Slideshare-icon Views on SlideShare
583
Embed Views
5

Actions

Likes
0
Downloads
1
Comments
0

2 Embeds 5

http://www.linkedin.com 3
https://www.linkedin.com 2

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \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 MongoDB at GUL Presentation Transcript

  • 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 of tremendous• Open source NoSQL database • Written in C++ • https://github.com/mongodb/mongo
  • ProductionDeployments
  • Format• BSON: Binary encoded serialization of JSON documents• Characteristics • Lightweight: minimum overhead • Traversable • Efficient: encoding and decoding
  • JSON{ _id : Object(), name : “mongoDB”, date : Date(), presenter: “isra”, attendants : [ {name:”ana”, age:23}, {name:”luis”, age: 32} ]}
  • Data schema• Database Database Collection• Collection Document• Documents (JSON) { user: 1, name: [] }
  • 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, automatic fail- over
  • Drivers• Officially supported • C, C++, Erlang, Haskell, Java, Javascript, .NET, Perl, PHP, Python, Ruby, Scala• Community supported • ActionScript, C#, Delphi, etc.
  • 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
  • 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”, attendants : [ {name:”ana”, age:23}, {name:”luis”, age: 32} ]})
  • 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});
  • 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()
  • 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)} });
  • 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}});
  • 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”);
  • 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; } });
  • 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)}} });
  • 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; } });
  • 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()}; } });
  • 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}});
  • 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:”isra”, age:31, languages:”js”} db.gul.find({“attendants.name”:isra._id})
  • 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 ))
  • 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
  • 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); } }
  • 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; }
  • Map/reduce (IV)• Número de cursos asistidos por persona db.gul.mapReduce({ map: map, reduce: reduce, {out: “my_new_collection”} });
  • Schema Design• Function of the data and the use case• Decisions • # of collections • Embedding or linking • Indexes • Sharding
  • 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 auto-sharding
  • Sharding• MongoDB offers sharding • Auto balancing • Easy addition of new machines • Up to 1k nodes • No single point of failure • Automatic failover
  • 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/
  • 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/
  • Thank you very much! Any questions?