SlideShare una empresa de Scribd logo
1 de 82
Descargar para leer sin conexión
SCALE




 Full
Metal
Mongo
• 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
Production
Deployments
Outline
• Terminology and            • Schema design
  basics
                             • Indexes
• The mongo shell
                             • DBA stuff
• Insert / update / delete
                             • Security
• Querying
                             • Replica sets
• Aggregation
                             • Sharding
• Map/reduce
Terminology and basics
Terminology

• NoSQL is almost everything
• Schemaless is nonesense : mongoDB do
  have a schema
 • Flexible
 • But a schema
Scaling out
 scale
speed
           NoSQL




                       features
Format

• BSON: Binary encoded serialization of
  JSON documents
• Characteristics
 • Lightweight: minimum overhead
 • Traversable
 • Efficient: encoding and decoding
JSON
{
    _id : ObjectId(xxxxx),
    name : 'Full Metal Mongo',
    date : Date(),
    presenter: 'isra',
    attendants : [
       {name:'ana', age:23},
       {name:'luis', age: 32}
     ]
}
    //default _id: 24 hex chars
Data schema
            Database
  Collection

    Document

{ user: 1, name: [] }
Collection
• Flexible: no fixed structure
 • ALTER TABLE (implicit)
• Created in the first insertion (same for
  dbs)
• Capped collection: maintain insert order,
  fixed size
Document

• JSON document
 • _id (ObjectId)
    • unique for the collection
    • it can be a document itself
 • Fields: numeric, string, date
 • Arrays and subdocuments
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.
• http://api.mongodb.org/
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
The mongo shell
Hands on:
      let’s get started

• Run a mongod (--fork) instance
• Run a mongo shell (mongo) that connects
  to this instance
The mongo shell:
          basics
• show dbs
• use db_name
• show collections (current db)
• show users (current db)
Insertion
Suppose a collection of GUL courses.

         db.courses.insert ({
           name : 'Full Metal Mongo',
           date : new Date(),
           presenter: 'isra',
           attendants : [
             {name: 'ana', age: 23},
             {name: 'luis', age: 32}
           ]
         }
Querying

//Full Metal Mongo course
db.gul.find({name:'Full Metal Mongo'})

//Courses attended by ana
db.gul.find({attendants.name:'ana'})

//Course names given by isra
db.gul.find({presenter:'isra'}, {name:1})
Querying II
//Courses ordered by name
db.gul.find().sort({name:1});

//The first 5 courses
db.gul.find().limit(5);

//Next five courses
db.gul.find().skip(5).limit(5);

//First course (natural order)
db.gul.findOne()
Querying III
//Courses attended by any under-age
db.gul.find({attendants.age:{$lt:18}});

//Last year courses between Monday and Thursday
db.gul.find({date:{
  $gt:new Date(2012,03,08),
  $lt:new Date(2012,03,11)}
});
Querying IV
//Courses attended by pedro or ana
db.gul.find({'attendants.name':
  {$in:['pedro', 'ana']}
});

//Courses attended by 10 people
db.gul.find({attendants:
  {$size:10}
});
$ operators
• $in / $nin              • $exists
• $all (default is any)   • $regex
• $gt(e) / $lt(e)         • $natural (order)
• $ne                     • $toLower / $toUpper
• $elemMatch
  (conditions in the
  same subdoc)
More $ expressions
• $sum      • $push (insert)
• $avg      • $addToSet (insert)
• $min      • $first (sort)
• $max      • $last (sort)
Update
//updates if exits; inserts if new
db.gul.save(x)

//update speakers in the crafty course
db.gul.update(
  {name:'Crafty'},
  {$set:{presenter:['javi','isra']}}
);

//new attendant to a course (not multi)
db.gul.update(
  {name:'mongoDB'},
  {attendants:
    {$push:{name:'pepe', age:19}}
  }
);
Find and Modify
• findAndModify (not widely used)
Remove
//removes all
db.gul.remove()

//search and remove
db.gul.remove({presenter:'isra'})
Database references:
     direct linking
//Query
isra = db.gul_members.findOne()

//Response from the query
{_id: ObjectId('ad234fea23482348'),
name:'isra', age:31, languages:'js'}

//Find by id
db.gul.find({'attendants._id':isra._id})
Database references:
         DBRef
//Query
isra = db.gul_members.findOne()

//Response
{_id: ObjectId('ad234fea23482348'),
name:'isra', age:31, languages:'js'}

//Insert by DBRef
db.gul.insert({
  name: 'mongoDB',
  presenter: new DBRef('gul_members',isra._id)
})
Import
              example data
     • Download a short courses collection from
      • http://www.it.uc3m.es/igrojas/mongo/
         initDB.json

//Import dataset in JSON
mongoimport --db gul --collection courses initDB.json
Hands on:
             querying
• Add a new course with data similar to the
  existing
• Update your course to add attendants
• Query courses with speaker “Jesús Espino”
• Query course on Friday
• Query courses tagged as “android”
Aggregation
    db.gul.aggregate([ pipeline ])
•    Pipelines (7)                  •   $order (1:1)

    •   $match (n:1)                •   $limit (n:1)

    •   $project (1:1)              •   $skip (n:1)

    •   $group (n:1)                •   $unwind (1:n)

    Examples: http://docs.mongodb.org/manual/reference/
    sql-aggregation-comparison/
Aggregation I

//Number of courses
db.gul.count();

//Number of courses given by isra
db.gul.count({presenter:'isra'});

//Distinct attendants to all courses
db.gul.distinct('attendants.name');
Aggregation II
db.grades.aggregate([
  {$unwind:"$scores"},
  {$match:{"scores.type":{$ne:"quiz"}}},
  {$group:{
    _id:{class_id:"$class_id",
    student_id:"$student_id"},
    score:{$avg:"$scores.score"}}
  }},
  {$group:{
    _id:{class_id:"$_id.class_id"},
    score:{$avg:"$score"}
  }},
  {$sort: {score:-1}}
])
Hands on:
         aggregation

• Distinct course speakers
• Distinct tags and count
• Number of courses per weekday
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)
• Courses attended per individual
var map = function(){
  for(var i in this.attendants){
    emit(this.attendants[i].name,1);
  }
}
Map/reduce (III)
• Courses attended per individual
   var reduce = function(key, values){
       var sum=0;
       for (var i in values){
         sum+=values[i];
       }
       return sum;
   }
Map/reduce (IV)
• Courses attended per individual
      db.gul.mapReduce({
        map: map,
        reduce: reduce,
        {out: {inline:1},
        query:{initial_query}}
      });
Hands on:
          map/reduce
• Update the some courses to add
  attendants
• Get all the courses attended by individual

• Distinct tags and count
Schema design
Schema Design
• Function of the data and the use case
• Decisions
 • # of collections
 • Embedding or linking
 • Indexes
 • Sharding
Relationships
• Types
 • 1:1(person:resume)
 • 1:n (city:person, post:comments)
 • m:n (teacher:student)
• Doc limit: 16MB
• Examples: school, blog
Transactions

• No transactions
 • Redesign schema
 • Implement in SW
 • Tolerate no transactions
Schema design:
         examples
• Let’s design the schema for
 • courses
 • school
 • blog / twitter
 • foursquare
Indexes
Indexes
• Objective: Query optimization
• Used in the query itself and/or the ordering
• B-Tree indexes
• _id index is automatic (unique)
   db.gul.ensureIndex({ name:1 })

   db.gul.getIndexes()

   db.gul.stats() //Size of the index
Indexes (II)
• For arrays, the index is multikey (one
  index entry per array element)
• Field names are not in indexes
//Compound indexes
db.gul.ensureIndex({ name:1, age:1})

//For nested fields (subdocs)
db.gul.ensureIndex({ attendants.name:1 })
Indexes types
• default
• unique
   db.gul.ensureIndex({name:1}, {unique:1})


• sparse
   db.gul.ensureIndex({name:1}, {sparse:1})


• TTL (time to live)
• geospatial
Indexes options

• dropDups: drop duplicate keys when
  creating the index (converted in unique)
• background: created in the background on
  primary of the replica set, in the foreground
  on secondaries
More about Indexes
• Covered index
 • query covered completely by the index
• Selectivity of an index
• Explain
   db.gul.find().explain()


• Hints
   db.gul.find().hint({name:1})
Geospatial indexes

• 2d-only
• compound indexes may be used
  db.places.ensureIndex({'loc':'2d'})

  db.places.find({loc:{
    $near:[20,40],
    $maxDistance:2}
  }).limit(50)
Creating indexes:
       examples

• Optimize our courses database
 • Think of common queries
 • Implement the convenient indexes
DBAs stuff
Backups

• mongodump / mongorestore
• copy files using your own software
  (journaling enabled required)
• replica sets: backup from secondary
Commands

db.gul.runCommand('compact')

db.runCommand({compact:'gul'})

//Run a script from the command line
mongo < path/to/script.js
Profiler
• Log queries / commands
  mongod --profile 0/1/2 --slowms 100

  //0: no
  //1: slow queries
  //2: all queries
  //slowms: threshold for type 1
Profiler (II)
• From the mongo shell
  db.getProfilingLevel() // 0-1-2

  db.getProfilingStatus() // { "was" : 0,
  "slowms" : 100 }

  db.setProfilingLevel(1,1000)



• Data stored in system.profile collection
Kill operations
• db.currentOp()
 • in progress operations
• db.killOp(op_id)
• Don’t kill
 • write ops in secondaries
 • compact
 • internal ops
Commands for dbas
• mongotop
 • time of activity per collection
 • info about total, read, write, etc.
• mongostat (command line)
 • every x seconds
 • info about insert, update, delete,
    getmore, command, flushes, mapped,
    vsize, res, faults, etc.
Security tips
Security
• mongod/mongos --auth //not from
  localhost
• Add user
 • use admin
 • db.addUser(user, passwd, [readOnly])
• Auth
 • use admin
• db.auth(user, passwd)
Types of users
• admin
 • created in the admin db
 • access to all dbs
• regular
 • access a specific db
 • read/write or readOnly
Intra-cluster security


• For replica sets, to use non-auth (faster)
  communications among the nodes
• mongod --keyFile file --replSet
Replica sets
What is a replica set?

• Info replicated among several nodes
• 1 primary
• n secondaries (min 3, to get a majority)
• When a node falls, there’s election and a
  majority is needed to select a new primary
Types of nodes in a
       replica set
• Regular
• Arbiter: decides the primary in a election
• Delayed: cannot be elected primary
• Hidden: used for analytics (not primary)
Replica set
      configuration
rs.config({ _id: 'rs_name',
  members: [{_id:0, host:host0}, {_id:1,
host: host1}, {_id:2, host: host2}]})

rs.status()

rs.slaveOk() //read form secondaries

rs.isMaster() //check primary
Write concern
• Journal: list of operations (inserts, updates)
  done, saved in disk (permanent)
• getLastError (managed by the driver)
 • w: wait until write is saved in memory
    (the app receives ack) Used to detect
    errors, like violation of a unique.
  • j: wait until write is saved in the journal
Oplog and write
         concern
• oplog.rs: capped collection with the
  operations made in the replica set, stored
  in natural order
• write concern
 • w: n, means wait response of n nodes in a
    replica set
  • w: ‘majority’, wait for the majority of the
    nodes
Sharding
What is sharding?
• Scalability
• Horizontal partitioning of a database
• A BSON document stored in ONE shard
• Shard key
 • Not unique
 • No unique fields in the collection
• Mongo offers auto-sharding
What is sharding?
• Auto balancing
• Easy addition of new machines
• Up to 1k nodes
• No single point of failure
• Automatic failover
• Select a convenient shard key
Sharding config

• Need of config servers
 • store metadata about chunks
 • mongod --configsvr
• Need mongod “routers”
 • mongos (accessed by the apps)
Sharding operations
• chunk: range of the sharding key being in a
  shard
• operations
 • split: dividing a chunk to balance the size
    of the chunks
  • migrate: moving a chunk from a shard to
    another
Sharding diagram




 via: http://www.cloudifysource.org/2012/03/25/petclinic_deepdive.html
Shard key
            selection
• Examples: choose the shard key for
 • courses
 • school
 • blog / twitter
 • foursquare
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/
• mongodb MOOCs (dbas and developers)
 • http://education.10gen.com
Thank you very much!
   Any questions?

Más contenido relacionado

La actualidad más candente

MongoDB for Beginners
MongoDB for BeginnersMongoDB for Beginners
MongoDB for BeginnersEnoch Joshua
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBantoinegirbal
 
Webinar: Back to Basics: Thinking in Documents
Webinar: Back to Basics: Thinking in DocumentsWebinar: Back to Basics: Thinking in Documents
Webinar: Back to Basics: Thinking in DocumentsMongoDB
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBNosh Petigara
 
Basics of MongoDB
Basics of MongoDB Basics of MongoDB
Basics of MongoDB Habilelabs
 
Mongodb - NoSql Database
Mongodb - NoSql DatabaseMongodb - NoSql Database
Mongodb - NoSql DatabasePrashant Gupta
 
2011 Mongo FR - MongoDB introduction
2011 Mongo FR - MongoDB introduction2011 Mongo FR - MongoDB introduction
2011 Mongo FR - MongoDB introductionantoinegirbal
 
Introduction to MongoDB and Hadoop
Introduction to MongoDB and HadoopIntroduction to MongoDB and Hadoop
Introduction to MongoDB and HadoopSteven Francia
 
Mongo db – document oriented database
Mongo db – document oriented databaseMongo db – document oriented database
Mongo db – document oriented databaseWojciech Sznapka
 
Indexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleIndexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleMongoDB
 

La actualidad más candente (17)

Mongo db basics
Mongo db basicsMongo db basics
Mongo db basics
 
MongoDB
MongoDBMongoDB
MongoDB
 
MongoDB for Beginners
MongoDB for BeginnersMongoDB for Beginners
MongoDB for Beginners
 
MongoDB 101
MongoDB 101MongoDB 101
MongoDB 101
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
Webinar: Back to Basics: Thinking in Documents
Webinar: Back to Basics: Thinking in DocumentsWebinar: Back to Basics: Thinking in Documents
Webinar: Back to Basics: Thinking in Documents
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
Basics of MongoDB
Basics of MongoDB Basics of MongoDB
Basics of MongoDB
 
Mongodb - NoSql Database
Mongodb - NoSql DatabaseMongodb - NoSql Database
Mongodb - NoSql Database
 
Mongo db
Mongo dbMongo db
Mongo db
 
MongoDB and hadoop
MongoDB and hadoopMongoDB and hadoop
MongoDB and hadoop
 
Mongo db
Mongo dbMongo db
Mongo db
 
2011 Mongo FR - MongoDB introduction
2011 Mongo FR - MongoDB introduction2011 Mongo FR - MongoDB introduction
2011 Mongo FR - MongoDB introduction
 
Introduction to MongoDB and Hadoop
Introduction to MongoDB and HadoopIntroduction to MongoDB and Hadoop
Introduction to MongoDB and Hadoop
 
MongoDB
MongoDBMongoDB
MongoDB
 
Mongo db – document oriented database
Mongo db – document oriented databaseMongo db – document oriented database
Mongo db – document oriented database
 
Indexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleIndexing Strategies to Help You Scale
Indexing Strategies to Help You Scale
 

Destacado

Final version mango language database presentation
Final version mango language database presentationFinal version mango language database presentation
Final version mango language database presentationSTCC Library
 
Mongo DB Terms - Mentormate Academy
Mongo DB Terms - Mentormate AcademyMongo DB Terms - Mentormate Academy
Mongo DB Terms - Mentormate AcademyDimitar Danailov
 
MAGICDB : Mango Genetic stocks Identification and Characterisation Data Base
MAGICDB : Mango Genetic stocks Identification and Characterisation Data BaseMAGICDB : Mango Genetic stocks Identification and Characterisation Data Base
MAGICDB : Mango Genetic stocks Identification and Characterisation Data BaseSenthil Natesan
 
Genetic transformation in mango
Genetic transformation in mangoGenetic transformation in mango
Genetic transformation in mangoRajesh Pati
 
Rest api design by george reese
Rest api design by george reeseRest api design by george reese
Rest api design by george reesebuildacloud
 
Brochure Finovation 2014
Brochure Finovation 2014Brochure Finovation 2014
Brochure Finovation 2014inovenaltenor
 
MongoDB Auto-Sharding at Mongo Seattle
MongoDB Auto-Sharding at Mongo SeattleMongoDB Auto-Sharding at Mongo Seattle
MongoDB Auto-Sharding at Mongo SeattleMongoDB
 

Destacado (11)

Manual de como instalar mongo db en windows
Manual de  como instalar mongo db en windowsManual de  como instalar mongo db en windows
Manual de como instalar mongo db en windows
 
Final version mango language database presentation
Final version mango language database presentationFinal version mango language database presentation
Final version mango language database presentation
 
Mongo DB Terms - Mentormate Academy
Mongo DB Terms - Mentormate AcademyMongo DB Terms - Mentormate Academy
Mongo DB Terms - Mentormate Academy
 
MAGICDB : Mango Genetic stocks Identification and Characterisation Data Base
MAGICDB : Mango Genetic stocks Identification and Characterisation Data BaseMAGICDB : Mango Genetic stocks Identification and Characterisation Data Base
MAGICDB : Mango Genetic stocks Identification and Characterisation Data Base
 
Mango
MangoMango
Mango
 
Genetic transformation in mango
Genetic transformation in mangoGenetic transformation in mango
Genetic transformation in mango
 
Mongo indexes
Mongo indexesMongo indexes
Mongo indexes
 
Rest api design by george reese
Rest api design by george reeseRest api design by george reese
Rest api design by george reese
 
Brochure Finovation 2014
Brochure Finovation 2014Brochure Finovation 2014
Brochure Finovation 2014
 
Mongo db
Mongo dbMongo db
Mongo db
 
MongoDB Auto-Sharding at Mongo Seattle
MongoDB Auto-Sharding at Mongo SeattleMongoDB Auto-Sharding at Mongo Seattle
MongoDB Auto-Sharding at Mongo Seattle
 

Similar a Full metal mongo

2012 mongo db_bangalore_roadmap_new
2012 mongo db_bangalore_roadmap_new2012 mongo db_bangalore_roadmap_new
2012 mongo db_bangalore_roadmap_newMongoDB
 
10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data ModelingDATAVERSITY
 
Mongodb intro
Mongodb introMongodb intro
Mongodb introchristkv
 
MongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overviewMongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overviewAntonio Pintus
 
Schema Design (Mongo Austin)
Schema Design (Mongo Austin)Schema Design (Mongo Austin)
Schema Design (Mongo Austin)MongoDB
 
2016 feb-23 pyugre-py_mongo
2016 feb-23 pyugre-py_mongo2016 feb-23 pyugre-py_mongo
2016 feb-23 pyugre-py_mongoMichael Bright
 
Using MongoDB and Python
Using MongoDB and PythonUsing MongoDB and Python
Using MongoDB and PythonMike Bright
 
PHP Development With MongoDB
PHP Development With MongoDBPHP Development With MongoDB
PHP Development With MongoDBFitz Agard
 
PHP Development with MongoDB (Fitz Agard)
PHP Development with MongoDB (Fitz Agard)PHP Development with MongoDB (Fitz Agard)
PHP Development with MongoDB (Fitz Agard)MongoSF
 
Intro to MongoDB and datamodeling
Intro to MongoDB and datamodeling Intro to MongoDB and datamodeling
Intro to MongoDB and datamodeling rogerbodamer
 
OSDC 2012 | Building a first application on MongoDB by Ross Lawley
OSDC 2012 | Building a first application on MongoDB by Ross LawleyOSDC 2012 | Building a first application on MongoDB by Ross Lawley
OSDC 2012 | Building a first application on MongoDB by Ross LawleyNETWAYS
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBSean Laurent
 
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!Daniel Cousineau
 
Schema Design with MongoDB
Schema Design with MongoDBSchema Design with MongoDB
Schema Design with MongoDBrogerbodamer
 
The Fine Art of Schema Design in MongoDB: Dos and Don'ts
The Fine Art of Schema Design in MongoDB: Dos and Don'tsThe Fine Art of Schema Design in MongoDB: Dos and Don'ts
The Fine Art of Schema Design in MongoDB: Dos and Don'tsMatias Cascallares
 
Webinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev TeamsWebinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev TeamsMongoDB
 
How to use MongoDB with CakePHP
How to use MongoDB with CakePHPHow to use MongoDB with CakePHP
How to use MongoDB with CakePHPichikaway
 

Similar a Full metal mongo (20)

MongoDB at GUL
MongoDB at GULMongoDB at GUL
MongoDB at GUL
 
2012 mongo db_bangalore_roadmap_new
2012 mongo db_bangalore_roadmap_new2012 mongo db_bangalore_roadmap_new
2012 mongo db_bangalore_roadmap_new
 
10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling
 
Mongodb intro
Mongodb introMongodb intro
Mongodb intro
 
MongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overviewMongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overview
 
Schema Design (Mongo Austin)
Schema Design (Mongo Austin)Schema Design (Mongo Austin)
Schema Design (Mongo Austin)
 
2016 feb-23 pyugre-py_mongo
2016 feb-23 pyugre-py_mongo2016 feb-23 pyugre-py_mongo
2016 feb-23 pyugre-py_mongo
 
Using MongoDB and Python
Using MongoDB and PythonUsing MongoDB and Python
Using MongoDB and Python
 
PHP Development With MongoDB
PHP Development With MongoDBPHP Development With MongoDB
PHP Development With MongoDB
 
PHP Development with MongoDB (Fitz Agard)
PHP Development with MongoDB (Fitz Agard)PHP Development with MongoDB (Fitz Agard)
PHP Development with MongoDB (Fitz Agard)
 
Intro to MongoDB and datamodeling
Intro to MongoDB and datamodeling Intro to MongoDB and datamodeling
Intro to MongoDB and datamodeling
 
OSDC 2012 | Building a first application on MongoDB by Ross Lawley
OSDC 2012 | Building a first application on MongoDB by Ross LawleyOSDC 2012 | Building a first application on MongoDB by Ross Lawley
OSDC 2012 | Building a first application on MongoDB by Ross Lawley
 
MongoDb and NoSQL
MongoDb and NoSQLMongoDb and NoSQL
MongoDb and NoSQL
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
 
Latinoware
LatinowareLatinoware
Latinoware
 
Schema Design with MongoDB
Schema Design with MongoDBSchema Design with MongoDB
Schema Design with MongoDB
 
The Fine Art of Schema Design in MongoDB: Dos and Don'ts
The Fine Art of Schema Design in MongoDB: Dos and Don'tsThe Fine Art of Schema Design in MongoDB: Dos and Don'ts
The Fine Art of Schema Design in MongoDB: Dos and Don'ts
 
Webinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev TeamsWebinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev Teams
 
How to use MongoDB with CakePHP
How to use MongoDB with CakePHPHow to use MongoDB with CakePHP
How to use MongoDB with CakePHP
 

Más de Israel Gutiérrez

Make startup development great again!
Make startup development great again!Make startup development great again!
Make startup development great again!Israel Gutiérrez
 
Emoticritico: midiendo las emociones de los políticos
Emoticritico: midiendo las emociones de los políticosEmoticritico: midiendo las emociones de los políticos
Emoticritico: midiendo las emociones de los políticosIsrael Gutiérrez
 
Boost your ProDADtivity: productivity tips for entrepreneurial dads and mums
Boost your ProDADtivity: productivity tips for entrepreneurial dads and mums Boost your ProDADtivity: productivity tips for entrepreneurial dads and mums
Boost your ProDADtivity: productivity tips for entrepreneurial dads and mums Israel Gutiérrez
 
Learning Analytics Support for Just-in-time Teaching
Learning Analytics Support for Just-in-time TeachingLearning Analytics Support for Just-in-time Teaching
Learning Analytics Support for Just-in-time TeachingIsrael Gutiérrez
 
Jugando con websockets en nodeJS
Jugando con websockets en nodeJSJugando con websockets en nodeJS
Jugando con websockets en nodeJSIsrael Gutiérrez
 
Transfórmate en un profesor con superpoderes
Transfórmate en un profesor con superpoderesTransfórmate en un profesor con superpoderes
Transfórmate en un profesor con superpoderesIsrael Gutiérrez
 
Enhancing orchestration of lab sessions by means of awareness mechanisms
Enhancing orchestration of lab sessions by means of awareness mechanismsEnhancing orchestration of lab sessions by means of awareness mechanisms
Enhancing orchestration of lab sessions by means of awareness mechanismsIsrael Gutiérrez
 
Pushing the awareness envelope
Pushing the awareness envelopePushing the awareness envelope
Pushing the awareness envelopeIsrael Gutiérrez
 
Orchestration and Feedback in Lab Sessions: ECTEL11
Orchestration and Feedback in Lab Sessions: ECTEL11Orchestration and Feedback in Lab Sessions: ECTEL11
Orchestration and Feedback in Lab Sessions: ECTEL11Israel Gutiérrez
 
JTELSS11 gradient presentation
JTELSS11 gradient presentationJTELSS11 gradient presentation
JTELSS11 gradient presentationIsrael Gutiérrez
 
Research questions by the Blueberries
Research questions by the BlueberriesResearch questions by the Blueberries
Research questions by the BlueberriesIsrael Gutiérrez
 
Management of Assessment Resources in a Federated Repository of Educational R...
Management of Assessment Resources in a Federated Repository of Educational R...Management of Assessment Resources in a Federated Repository of Educational R...
Management of Assessment Resources in a Federated Repository of Educational R...Israel Gutiérrez
 

Más de Israel Gutiérrez (16)

All you need is front
All you need is frontAll you need is front
All you need is front
 
Make startup development great again!
Make startup development great again!Make startup development great again!
Make startup development great again!
 
Emoticritico: midiendo las emociones de los políticos
Emoticritico: midiendo las emociones de los políticosEmoticritico: midiendo las emociones de los políticos
Emoticritico: midiendo las emociones de los políticos
 
Boost your ProDADtivity: productivity tips for entrepreneurial dads and mums
Boost your ProDADtivity: productivity tips for entrepreneurial dads and mums Boost your ProDADtivity: productivity tips for entrepreneurial dads and mums
Boost your ProDADtivity: productivity tips for entrepreneurial dads and mums
 
Learning Analytics Support for Just-in-time Teaching
Learning Analytics Support for Just-in-time TeachingLearning Analytics Support for Just-in-time Teaching
Learning Analytics Support for Just-in-time Teaching
 
Jugando con websockets en nodeJS
Jugando con websockets en nodeJSJugando con websockets en nodeJS
Jugando con websockets en nodeJS
 
Transfórmate en un profesor con superpoderes
Transfórmate en un profesor con superpoderesTransfórmate en un profesor con superpoderes
Transfórmate en un profesor con superpoderes
 
Enhancing orchestration of lab sessions by means of awareness mechanisms
Enhancing orchestration of lab sessions by means of awareness mechanismsEnhancing orchestration of lab sessions by means of awareness mechanisms
Enhancing orchestration of lab sessions by means of awareness mechanisms
 
Pushing the awareness envelope
Pushing the awareness envelopePushing the awareness envelope
Pushing the awareness envelope
 
Stay at KU Leuven
Stay at KU LeuvenStay at KU Leuven
Stay at KU Leuven
 
Orchestration and Feedback in Lab Sessions: ECTEL11
Orchestration and Feedback in Lab Sessions: ECTEL11Orchestration and Feedback in Lab Sessions: ECTEL11
Orchestration and Feedback in Lab Sessions: ECTEL11
 
The feedback loop revisited
The feedback loop revisitedThe feedback loop revisited
The feedback loop revisited
 
JTELSS11 gradient presentation
JTELSS11 gradient presentationJTELSS11 gradient presentation
JTELSS11 gradient presentation
 
Research questions by the Blueberries
Research questions by the BlueberriesResearch questions by the Blueberries
Research questions by the Blueberries
 
Seminario eMadrid
Seminario eMadridSeminario eMadrid
Seminario eMadrid
 
Management of Assessment Resources in a Federated Repository of Educational R...
Management of Assessment Resources in a Federated Repository of Educational R...Management of Assessment Resources in a Federated Repository of Educational R...
Management of Assessment Resources in a Federated Repository of Educational R...
 

Último

WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 

Último (20)

WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 

Full metal mongo

  • 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
  • 4. Outline • Terminology and • Schema design basics • Indexes • The mongo shell • DBA stuff • Insert / update / delete • Security • Querying • Replica sets • Aggregation • Sharding • Map/reduce
  • 6. Terminology • NoSQL is almost everything • Schemaless is nonesense : mongoDB do have a schema • Flexible • But a schema
  • 7. Scaling out scale speed NoSQL features
  • 8. Format • BSON: Binary encoded serialization of JSON documents • Characteristics • Lightweight: minimum overhead • Traversable • Efficient: encoding and decoding
  • 9. JSON { _id : ObjectId(xxxxx), name : 'Full Metal Mongo', date : Date(), presenter: 'isra', attendants : [ {name:'ana', age:23}, {name:'luis', age: 32} ] } //default _id: 24 hex chars
  • 10. Data schema Database Collection Document { user: 1, name: [] }
  • 11. Collection • Flexible: no fixed structure • ALTER TABLE (implicit) • Created in the first insertion (same for dbs) • Capped collection: maintain insert order, fixed size
  • 12. Document • JSON document • _id (ObjectId) • unique for the collection • it can be a document itself • Fields: numeric, string, date • Arrays and subdocuments
  • 13. SQL to Mongo mapping
  • 14. MongoDB basics • Default port: 27017 • Optional authentication • Data location: /data/db/ • Modes • automatic replication • automatic fail-over
  • 15. Drivers • Officially supported • C, C++, Erlang, Haskell, Java, Javascript, .NET, Perl, PHP, Python, Ruby, Scala • Community supported • ActionScript, C#, Delphi, etc. • http://api.mongodb.org/
  • 16. 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
  • 18. Hands on: let’s get started • Run a mongod (--fork) instance • Run a mongo shell (mongo) that connects to this instance
  • 19. The mongo shell: basics • show dbs • use db_name • show collections (current db) • show users (current db)
  • 20. Insertion Suppose a collection of GUL courses. db.courses.insert ({ name : 'Full Metal Mongo', date : new Date(), presenter: 'isra', attendants : [ {name: 'ana', age: 23}, {name: 'luis', age: 32} ] }
  • 21. Querying //Full Metal Mongo course db.gul.find({name:'Full Metal Mongo'}) //Courses attended by ana db.gul.find({attendants.name:'ana'}) //Course names given by isra db.gul.find({presenter:'isra'}, {name:1})
  • 22. Querying II //Courses ordered by name db.gul.find().sort({name:1}); //The first 5 courses db.gul.find().limit(5); //Next five courses db.gul.find().skip(5).limit(5); //First course (natural order) db.gul.findOne()
  • 23. Querying III //Courses attended by any under-age db.gul.find({attendants.age:{$lt:18}}); //Last year courses between Monday and Thursday db.gul.find({date:{ $gt:new Date(2012,03,08), $lt:new Date(2012,03,11)} });
  • 24. Querying IV //Courses attended by pedro or ana db.gul.find({'attendants.name': {$in:['pedro', 'ana']} }); //Courses attended by 10 people db.gul.find({attendants: {$size:10} });
  • 25. $ operators • $in / $nin • $exists • $all (default is any) • $regex • $gt(e) / $lt(e) • $natural (order) • $ne • $toLower / $toUpper • $elemMatch (conditions in the same subdoc)
  • 26. More $ expressions • $sum • $push (insert) • $avg • $addToSet (insert) • $min • $first (sort) • $max • $last (sort)
  • 27. Update //updates if exits; inserts if new db.gul.save(x) //update speakers in the crafty course db.gul.update( {name:'Crafty'}, {$set:{presenter:['javi','isra']}} ); //new attendant to a course (not multi) db.gul.update( {name:'mongoDB'}, {attendants: {$push:{name:'pepe', age:19}} } );
  • 28. Find and Modify • findAndModify (not widely used)
  • 29. Remove //removes all db.gul.remove() //search and remove db.gul.remove({presenter:'isra'})
  • 30. Database references: direct linking //Query isra = db.gul_members.findOne() //Response from the query {_id: ObjectId('ad234fea23482348'), name:'isra', age:31, languages:'js'} //Find by id db.gul.find({'attendants._id':isra._id})
  • 31. Database references: DBRef //Query isra = db.gul_members.findOne() //Response {_id: ObjectId('ad234fea23482348'), name:'isra', age:31, languages:'js'} //Insert by DBRef db.gul.insert({ name: 'mongoDB', presenter: new DBRef('gul_members',isra._id) })
  • 32. Import example data • Download a short courses collection from • http://www.it.uc3m.es/igrojas/mongo/ initDB.json //Import dataset in JSON mongoimport --db gul --collection courses initDB.json
  • 33. Hands on: querying • Add a new course with data similar to the existing • Update your course to add attendants • Query courses with speaker “Jesús Espino” • Query course on Friday • Query courses tagged as “android”
  • 34. Aggregation db.gul.aggregate([ pipeline ]) • Pipelines (7) • $order (1:1) • $match (n:1) • $limit (n:1) • $project (1:1) • $skip (n:1) • $group (n:1) • $unwind (1:n) Examples: http://docs.mongodb.org/manual/reference/ sql-aggregation-comparison/
  • 35. Aggregation I //Number of courses db.gul.count(); //Number of courses given by isra db.gul.count({presenter:'isra'}); //Distinct attendants to all courses db.gul.distinct('attendants.name');
  • 36. Aggregation II db.grades.aggregate([ {$unwind:"$scores"}, {$match:{"scores.type":{$ne:"quiz"}}}, {$group:{ _id:{class_id:"$class_id", student_id:"$student_id"}, score:{$avg:"$scores.score"}} }}, {$group:{ _id:{class_id:"$_id.class_id"}, score:{$avg:"$score"} }}, {$sort: {score:-1}} ])
  • 37. Hands on: aggregation • Distinct course speakers • Distinct tags and count • Number of courses per weekday
  • 38. 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
  • 39. Map/reduce (II) • Courses attended per individual var map = function(){ for(var i in this.attendants){ emit(this.attendants[i].name,1); } }
  • 40. Map/reduce (III) • Courses attended per individual var reduce = function(key, values){ var sum=0; for (var i in values){ sum+=values[i]; } return sum; }
  • 41. Map/reduce (IV) • Courses attended per individual db.gul.mapReduce({ map: map, reduce: reduce, {out: {inline:1}, query:{initial_query}} });
  • 42. Hands on: map/reduce • Update the some courses to add attendants • Get all the courses attended by individual • Distinct tags and count
  • 44. Schema Design • Function of the data and the use case • Decisions • # of collections • Embedding or linking • Indexes • Sharding
  • 45. Relationships • Types • 1:1(person:resume) • 1:n (city:person, post:comments) • m:n (teacher:student) • Doc limit: 16MB • Examples: school, blog
  • 46. Transactions • No transactions • Redesign schema • Implement in SW • Tolerate no transactions
  • 47. Schema design: examples • Let’s design the schema for • courses • school • blog / twitter • foursquare
  • 49. Indexes • Objective: Query optimization • Used in the query itself and/or the ordering • B-Tree indexes • _id index is automatic (unique) db.gul.ensureIndex({ name:1 }) db.gul.getIndexes() db.gul.stats() //Size of the index
  • 50. Indexes (II) • For arrays, the index is multikey (one index entry per array element) • Field names are not in indexes //Compound indexes db.gul.ensureIndex({ name:1, age:1}) //For nested fields (subdocs) db.gul.ensureIndex({ attendants.name:1 })
  • 51. Indexes types • default • unique db.gul.ensureIndex({name:1}, {unique:1}) • sparse db.gul.ensureIndex({name:1}, {sparse:1}) • TTL (time to live) • geospatial
  • 52. Indexes options • dropDups: drop duplicate keys when creating the index (converted in unique) • background: created in the background on primary of the replica set, in the foreground on secondaries
  • 53. More about Indexes • Covered index • query covered completely by the index • Selectivity of an index • Explain db.gul.find().explain() • Hints db.gul.find().hint({name:1})
  • 54. Geospatial indexes • 2d-only • compound indexes may be used db.places.ensureIndex({'loc':'2d'}) db.places.find({loc:{ $near:[20,40], $maxDistance:2} }).limit(50)
  • 55. Creating indexes: examples • Optimize our courses database • Think of common queries • Implement the convenient indexes
  • 57. Backups • mongodump / mongorestore • copy files using your own software (journaling enabled required) • replica sets: backup from secondary
  • 59. Profiler • Log queries / commands mongod --profile 0/1/2 --slowms 100 //0: no //1: slow queries //2: all queries //slowms: threshold for type 1
  • 60. Profiler (II) • From the mongo shell db.getProfilingLevel() // 0-1-2 db.getProfilingStatus() // { "was" : 0, "slowms" : 100 } db.setProfilingLevel(1,1000) • Data stored in system.profile collection
  • 61. Kill operations • db.currentOp() • in progress operations • db.killOp(op_id) • Don’t kill • write ops in secondaries • compact • internal ops
  • 62. Commands for dbas • mongotop • time of activity per collection • info about total, read, write, etc. • mongostat (command line) • every x seconds • info about insert, update, delete, getmore, command, flushes, mapped, vsize, res, faults, etc.
  • 64. Security • mongod/mongos --auth //not from localhost • Add user • use admin • db.addUser(user, passwd, [readOnly]) • Auth • use admin • db.auth(user, passwd)
  • 65. Types of users • admin • created in the admin db • access to all dbs • regular • access a specific db • read/write or readOnly
  • 66. Intra-cluster security • For replica sets, to use non-auth (faster) communications among the nodes • mongod --keyFile file --replSet
  • 68. What is a replica set? • Info replicated among several nodes • 1 primary • n secondaries (min 3, to get a majority) • When a node falls, there’s election and a majority is needed to select a new primary
  • 69. Types of nodes in a replica set • Regular • Arbiter: decides the primary in a election • Delayed: cannot be elected primary • Hidden: used for analytics (not primary)
  • 70. Replica set configuration rs.config({ _id: 'rs_name', members: [{_id:0, host:host0}, {_id:1, host: host1}, {_id:2, host: host2}]}) rs.status() rs.slaveOk() //read form secondaries rs.isMaster() //check primary
  • 71. Write concern • Journal: list of operations (inserts, updates) done, saved in disk (permanent) • getLastError (managed by the driver) • w: wait until write is saved in memory (the app receives ack) Used to detect errors, like violation of a unique. • j: wait until write is saved in the journal
  • 72. Oplog and write concern • oplog.rs: capped collection with the operations made in the replica set, stored in natural order • write concern • w: n, means wait response of n nodes in a replica set • w: ‘majority’, wait for the majority of the nodes
  • 74. What is sharding? • Scalability • Horizontal partitioning of a database • A BSON document stored in ONE shard • Shard key • Not unique • No unique fields in the collection • Mongo offers auto-sharding
  • 75. What is sharding? • Auto balancing • Easy addition of new machines • Up to 1k nodes • No single point of failure • Automatic failover • Select a convenient shard key
  • 76. Sharding config • Need of config servers • store metadata about chunks • mongod --configsvr • Need mongod “routers” • mongos (accessed by the apps)
  • 77. Sharding operations • chunk: range of the sharding key being in a shard • operations • split: dividing a chunk to balance the size of the chunks • migrate: moving a chunk from a shard to another
  • 78. Sharding diagram via: http://www.cloudifysource.org/2012/03/25/petclinic_deepdive.html
  • 79. Shard key selection • Examples: choose the shard key for • courses • school • blog / twitter • foursquare
  • 80. 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/
  • 81. 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/ • mongodb MOOCs (dbas and developers) • http://education.10gen.com
  • 82. Thank you very much! Any questions?