full metal mongo
DESCRIPTION
Full metal mongo course at GUL uc3mTRANSCRIPT
![Page 1: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/1.jpg)
FullMetalMongo
SCALE
![Page 2: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/2.jpg)
• 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
![Page 3: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/3.jpg)
Production Deployments
![Page 4: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/4.jpg)
Outline• Terminology and
basics
• The mongo shell
• Insert / update / delete
• Querying
• Aggregation
• Map/reduce
• Schema design
• Indexes
• DBA stuff
• Security
• Replica sets
• Sharding
![Page 5: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/5.jpg)
Terminology and basics
![Page 6: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/6.jpg)
Terminology
• NoSQL is almost everything
• Schemaless is nonesense : mongoDB do have a schema
• Flexible
• But a schema
![Page 7: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/7.jpg)
Scaling out
scalespeed
features
NoSQL
![Page 8: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/8.jpg)
Format
• BSON: Binary encoded serialization of JSON documents
• Characteristics
• Lightweight: minimum overhead
• Traversable
• Efficient: encoding and decoding
![Page 9: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/9.jpg)
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
![Page 10: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/10.jpg)
Data schema
DatabaseCollection
Document
{ user: 1, name: [] }
![Page 11: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/11.jpg)
Collection
• Flexible: no fixed structure
• ALTER TABLE (implicit)
• Created in the first insertion (same for dbs)
• Capped collection: maintain insert order, fixed size
![Page 12: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/12.jpg)
Document
• JSON document
• _id (ObjectId)
• unique for the collection
• it can be a document itself
• Fields: numeric, string, date
• Arrays and subdocuments
![Page 13: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/13.jpg)
SQL to Mongo mapping
![Page 14: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/14.jpg)
MongoDB basics
• Default port: 27017
• Optional authentication
• Data location: /data/db/
• Modes
• automatic replication
• automatic fail-over
![Page 15: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/15.jpg)
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/
![Page 16: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/16.jpg)
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
![Page 17: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/17.jpg)
The mongo shell
![Page 18: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/18.jpg)
Hands on:let’s get started
• Run a mongod (--fork) instance
• Run a mongo shell (mongo) that connects to this instance
![Page 19: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/19.jpg)
The mongo shell: basics
• show dbs
• use db_name
• show collections (current db)
• show users (current db)
![Page 20: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/20.jpg)
InsertionSuppose 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} ]}
![Page 21: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/21.jpg)
Querying
//Full Metal Mongo coursedb.gul.find({name:'Full Metal Mongo'})
//Courses attended by anadb.gul.find({attendants.name:'ana'})
//Course names given by isradb.gul.find({presenter:'isra'}, {name:1})
![Page 22: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/22.jpg)
Querying II//Courses ordered by namedb.gul.find().sort({name:1});
//The first 5 coursesdb.gul.find().limit(5);
//Next five coursesdb.gul.find().skip(5).limit(5);
//First course (natural order)db.gul.findOne()
![Page 23: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/23.jpg)
Querying III//Courses attended by any under-agedb.gul.find({attendants.age:{$lt:18}});
//Last year courses between Monday and Thursdaydb.gul.find({date:{ $gt:new Date(2012,03,08), $lt:new Date(2012,03,11)}});
![Page 24: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/24.jpg)
Querying IV
//Courses attended by pedro or anadb.gul.find({'attendants.name':{$in:['pedro', 'ana']}
});
//Courses attended by 10 peopledb.gul.find({attendants:{$size:10}
});
![Page 25: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/25.jpg)
$ operators• $in / $nin
• $all (default is any)
• $gt(e) / $lt(e)
• $ne
• $elemMatch (conditions in the same subdoc)
• $exists
• $regex
• $natural (order)
• $toLower / $toUpper
![Page 26: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/26.jpg)
More $ expressions
• $sum
• $avg
• $min
• $max
• $push (insert)
• $addToSet (insert)
• $first (sort)
• $last (sort)
![Page 27: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/27.jpg)
Update//updates if exits; inserts if newdb.gul.save(x)
//update speakers in the crafty coursedb.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}}}
);
![Page 28: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/28.jpg)
Find and Modify
• findAndModify (not widely used)
![Page 29: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/29.jpg)
Remove
//removes alldb.gul.remove()
//search and removedb.gul.remove({presenter:'isra'})
![Page 30: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/30.jpg)
Database references: direct linking
//Queryisra = db.gul_members.findOne()
//Response from the query{_id: ObjectId('ad234fea23482348'), name:'isra', age:31, languages:'js'}
//Find by iddb.gul.find({'attendants._id':isra._id})
![Page 31: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/31.jpg)
Database references: DBRef
//Queryisra = db.gul_members.findOne()
//Response{_id: ObjectId('ad234fea23482348'), name:'isra', age:31, languages:'js'}
//Insert by DBRefdb.gul.insert({name: 'mongoDB', presenter: new DBRef('gul_members',isra._id)
})
![Page 32: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/32.jpg)
Import example data
//Import dataset in JSONmongoimport --db gul --collection courses initDB.json
• Download a short courses collection from
• http://www.it.uc3m.es/igrojas/mongo/initDB.json
![Page 33: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/33.jpg)
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”
![Page 34: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/34.jpg)
Aggregation
• Pipelines (7)
• $match (n:1)
• $project (1:1)
• $group (n:1)
• $order (1:1)
• $limit (n:1)
• $skip (n:1)
• $unwind (1:n)
Examples: http://docs.mongodb.org/manual/reference/sql-aggregation-comparison/
db.gul.aggregate([ pipeline ])
![Page 35: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/35.jpg)
Aggregation I
//Number of coursesdb.gul.count();
//Number of courses given by isradb.gul.count({presenter:'isra'});
//Distinct attendants to all coursesdb.gul.distinct('attendants.name');
![Page 36: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/36.jpg)
Aggregation IIdb.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}}
])
![Page 37: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/37.jpg)
Hands on:aggregation
• Distinct course speakers
• Distinct tags and count
• Number of courses per weekday
![Page 38: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/38.jpg)
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
![Page 39: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/39.jpg)
Map/reduce (II)
• Courses attended per individual
var map = function(){ for(var i in this.attendants){ emit(this.attendants[i].name,1); }}
![Page 40: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/40.jpg)
Map/reduce (III)
var reduce = function(key, values){ var sum=0; for (var i in values){ sum+=values[i]; } return sum;}
• Courses attended per individual
![Page 41: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/41.jpg)
Map/reduce (IV)
• Courses attended per individual db.gul.mapReduce({
map: map, reduce: reduce, {out: {inline:1},query:{initial_query}}
});
![Page 42: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/42.jpg)
Hands on:map/reduce
• Update the some courses to add attendants
• Get all the courses attended by individual
• Distinct tags and count
![Page 43: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/43.jpg)
Schema design
![Page 44: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/44.jpg)
Schema Design
• Function of the data and the use case
• Decisions
• # of collections
• Embedding or linking
• Indexes
• Sharding
![Page 45: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/45.jpg)
Relationships
• Types
• 1:1(person:resume)
• 1:n (city:person, post:comments)
• m:n (teacher:student)
• Doc limit: 16MB
• Examples: school, blog
![Page 46: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/46.jpg)
Transactions
• No transactions
• Redesign schema
• Implement in SW
• Tolerate no transactions
![Page 47: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/47.jpg)
Schema design: examples
• Let’s design the schema for
• courses
• school
• blog / twitter
• foursquare
![Page 48: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/48.jpg)
Indexes
![Page 49: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/49.jpg)
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
![Page 50: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/50.jpg)
Indexes (II)
• For arrays, the index is multikey (one index entry per array element)
• Field names are not in indexes
//Compound indexesdb.gul.ensureIndex({ name:1, age:1})
//For nested fields (subdocs)db.gul.ensureIndex({ attendants.name:1 })
![Page 51: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/51.jpg)
Indexes types
• default
• unique
• sparse
• TTL (time to live)
• geospatial
db.gul.ensureIndex({name:1}, {unique:1})
db.gul.ensureIndex({name:1}, {sparse:1})
![Page 52: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/52.jpg)
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
![Page 53: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/53.jpg)
More about Indexes
• Covered index
• query covered completely by the index
• Selectivity of an index
• Explain
• Hints
db.gul.find().explain()
db.gul.find().hint({name:1})
![Page 54: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/54.jpg)
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)
![Page 55: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/55.jpg)
Creating indexes: examples
• Optimize our courses database
• Think of common queries
• Implement the convenient indexes
![Page 56: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/56.jpg)
DBAs stuff
![Page 57: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/57.jpg)
Backups
• mongodump / mongorestore
• copy files using your own software (journaling enabled required)
• replica sets: backup from secondary
![Page 58: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/58.jpg)
Commands
db.gul.runCommand('compact')
db.runCommand({compact:'gul'})
//Run a script from the command linemongo < path/to/script.js
![Page 59: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/59.jpg)
Profiler
• Log queries / commands
mongod --profile 0/1/2 --slowms 100
//0: no//1: slow queries//2: all queries//slowms: threshold for type 1
![Page 60: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/60.jpg)
Profiler (II)
• From the mongo shell
• Data stored in system.profile collection
db.getProfilingLevel() // 0-1-2
db.getProfilingStatus() // { "was" : 0, "slowms" : 100 }
db.setProfilingLevel(1,1000)
![Page 61: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/61.jpg)
Kill operations
• db.currentOp()
• in progress operations
• db.killOp(op_id)
• Don’t kill
• write ops in secondaries
• compact
• internal ops
![Page 62: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/62.jpg)
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.
![Page 63: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/63.jpg)
Security tips
![Page 64: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/64.jpg)
Security• mongod/mongos --auth //not from
localhost
• Add user
• use admin
• db.addUser(user, passwd, [readOnly])
• Auth
• use admin
• db.auth(user, passwd)
![Page 65: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/65.jpg)
Types of users
• admin
• created in the admin db
• access to all dbs
• regular
• access a specific db
• read/write or readOnly
![Page 66: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/66.jpg)
Intra-cluster security
• For replica sets, to use non-auth (faster) communications among the nodes
• mongod --keyFile file --replSet
![Page 67: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/67.jpg)
Replica sets
![Page 68: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/68.jpg)
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
![Page 69: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/69.jpg)
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)
![Page 70: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/70.jpg)
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
![Page 71: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/71.jpg)
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
![Page 72: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/72.jpg)
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
![Page 73: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/73.jpg)
Sharding
![Page 74: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/74.jpg)
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
![Page 75: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/75.jpg)
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
![Page 76: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/76.jpg)
Sharding config
• Need of config servers
• store metadata about chunks
• mongod --configsvr
• Need mongod “routers”
• mongos (accessed by the apps)
![Page 77: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/77.jpg)
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
![Page 78: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/78.jpg)
Sharding diagram
via: http://www.cloudifysource.org/2012/03/25/petclinic_deepdive.html
![Page 79: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/79.jpg)
Shard key selection
• Examples: choose the shard key for
• courses
• school
• blog / twitter
• foursquare
![Page 80: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/80.jpg)
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/
![Page 81: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/81.jpg)
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
![Page 82: Full metal mongo](https://reader030.vdocuments.mx/reader030/viewer/2022013105/5562526bd8b42aa52d8b50e2/html5/thumbnails/82.jpg)
Thank you very much!Any questions?