SlideShare une entreprise Scribd logo
1  sur  82
Télécharger pour lire hors ligne
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?

Contenu connexe

Tendances

08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadata08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadatarehaniltifat
 
EXPLicando o Explain no PostgreSQL
EXPLicando o Explain no PostgreSQLEXPLicando o Explain no PostgreSQL
EXPLicando o Explain no PostgreSQLFabrízio Mello
 
Dimensional modeling in oracle sql developer
Dimensional modeling in oracle sql developerDimensional modeling in oracle sql developer
Dimensional modeling in oracle sql developerJeff Smith
 
Modelo Relacional (Base de Datos)
Modelo Relacional (Base de Datos)Modelo Relacional (Base de Datos)
Modelo Relacional (Base de Datos)Neguib Núñez
 
Intro To Mongo Db
Intro To Mongo DbIntro To Mongo Db
Intro To Mongo Dbchriskite
 
Mongodb basics and architecture
Mongodb basics and architectureMongodb basics and architecture
Mongodb basics and architectureBishal Khanal
 
Spring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutesSpring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutesVMware Tanzu
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals INick Buytaert
 
PostgreSQL- An Introduction
PostgreSQL- An IntroductionPostgreSQL- An Introduction
PostgreSQL- An IntroductionSmita Prasad
 
Indexing & Query Optimization
Indexing & Query OptimizationIndexing & Query Optimization
Indexing & Query OptimizationMongoDB
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQLrehaniltifat
 
Building and managing complex dependencies pipeline using Apache Oozie
Building and managing complex dependencies pipeline using Apache OozieBuilding and managing complex dependencies pipeline using Apache Oozie
Building and managing complex dependencies pipeline using Apache OozieDataWorks Summit/Hadoop Summit
 
An Introduction To NoSQL & MongoDB
An Introduction To NoSQL & MongoDBAn Introduction To NoSQL & MongoDB
An Introduction To NoSQL & MongoDBLee Theobald
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBMongoDB
 

Tendances (20)

08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadata08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadata
 
EXPLicando o Explain no PostgreSQL
EXPLicando o Explain no PostgreSQLEXPLicando o Explain no PostgreSQL
EXPLicando o Explain no PostgreSQL
 
Postgresql
PostgresqlPostgresql
Postgresql
 
Consultas básicas en sql server
Consultas básicas en sql serverConsultas básicas en sql server
Consultas básicas en sql server
 
Exprimiendo el ORM de Django
Exprimiendo el ORM de DjangoExprimiendo el ORM de Django
Exprimiendo el ORM de Django
 
Dimensional modeling in oracle sql developer
Dimensional modeling in oracle sql developerDimensional modeling in oracle sql developer
Dimensional modeling in oracle sql developer
 
Modelo Relacional (Base de Datos)
Modelo Relacional (Base de Datos)Modelo Relacional (Base de Datos)
Modelo Relacional (Base de Datos)
 
NoSQL - MongoDB
NoSQL - MongoDBNoSQL - MongoDB
NoSQL - MongoDB
 
Intro To Mongo Db
Intro To Mongo DbIntro To Mongo Db
Intro To Mongo Db
 
Mongodb basics and architecture
Mongodb basics and architectureMongodb basics and architecture
Mongodb basics and architecture
 
Ceh
CehCeh
Ceh
 
Spring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutesSpring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutes
 
PostgreSQL
PostgreSQLPostgreSQL
PostgreSQL
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals I
 
PostgreSQL- An Introduction
PostgreSQL- An IntroductionPostgreSQL- An Introduction
PostgreSQL- An Introduction
 
Indexing & Query Optimization
Indexing & Query OptimizationIndexing & Query Optimization
Indexing & Query Optimization
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQL
 
Building and managing complex dependencies pipeline using Apache Oozie
Building and managing complex dependencies pipeline using Apache OozieBuilding and managing complex dependencies pipeline using Apache Oozie
Building and managing complex dependencies pipeline using Apache Oozie
 
An Introduction To NoSQL & MongoDB
An Introduction To NoSQL & MongoDBAn Introduction To NoSQL & MongoDB
An Introduction To NoSQL & MongoDB
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 

En vedette

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
 
MongoDB for Beginners
MongoDB for BeginnersMongoDB for Beginners
MongoDB for BeginnersEnoch Joshua
 
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
 

En vedette (12)

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
 
MongoDB for Beginners
MongoDB for BeginnersMongoDB for Beginners
MongoDB for Beginners
 
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
 

Similaire à 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
 
Indexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleIndexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleMongoDB
 
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
 

Similaire à 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
 
Indexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleIndexing Strategies to Help You Scale
Indexing Strategies to Help You Scale
 
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
 

Plus 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
 

Plus 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...
 

Dernier

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
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Principled Technologies
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
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
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 

Dernier (20)

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
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
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
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 

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?