dinner in the sky€¦ · $ ./mongo > db.things.insert( { _id : 1, tags : ['dog',...
TRANSCRIPT
dinner in the sky
with
@marcboeker
boarding
MongoDB
MongoDB
freebie
Document-orientated Storage
JSON-Style Documents
Document-orientated Storage
JSON-Style Documents
Document-orientated Storage
Scales to Hell
JSON-Style Documents
Sharding/Replica Sets
Document-orientated Storage
Scales to Hell
JSON-Style Documents
Sharding/Replica Sets
Document-orientated Storage
Scales to Hell
Replication/Failover
JSON-Style Documents
Sharding/Replica Sets
MongoDB ♥ You
Document-orientated Storage
Scales to Hell
Replication/Failover
JSON-Style Documents
Sharding/Replica Sets
MongoDB ♥ You
Document-orientated Storage
Scales to Hell
Replication/Failover
Map & Reduce
welcome passengers
$ sudo port install mongodb
Installation on *nix
deb http://downloads.mongodb.org/distros/ubuntu 10.4 10gen
$ sudo apt-get install mongodb
Ruby
♥ Ruby
$ sudo gem install mongo
Python
♥ Python
$ sudo easy_install pymongo
Node.js
♥ Node.js
$ sudo npm install mongodb
♥
$ sudo pecl install mongo
$ echo "extension=mongo.so" >> php.ini
taking off
$ mongo
db.foo.insert()
db.foo.insert({ name: "Angie", buddies: ["Wulfi", "Sarrazi", "Obami"], followers: 14.75, marked_as_spam: true});
db.foo.find()
db.foo.find({name: "Angie"});{ "_id" : ObjectId("4c9f87f54d17321d092991aa"), "name" : "Angie", "buddies" : ["Wulfi", "Sarrazi","Obami"], "followers" : 14.75, "marked_as_spam" : true}
db.foo.find({name: "Angie"});{ "_id" : ObjectId("4c9f87f54d17321d092991aa"), "name" : "Angie", "buddies" : ["Wulfi", "Sarrazi","Obami"], "followers" : 14.75, "marked_as_spam" : true}
magic
db.foo.update()
db.foo.update( {name: "Angie"}, {"$push": {buddies: "Puti"}});
db.foo.update( {name: "Angie"}, {"$push": {buddies: "Puti"}});
db.foo.find({name: "Angie"});{ "_id" : ObjectId("4c9f87f54d17321d092991aa"), "name" : "Angie", "buddies" : ["Wulfi", "Sarrazi","Obami", "Puti"], "followers" : 14.75, "marked_as_spam" : true}
db.foo.remove()
db.foo.remove({name: "Angie"});
db.foo.remove({name: "Angie"});
db.foo.remove({buddies: "Sarrazi"});
db.foo.remove({name: "Angie"});
db.foo.remove({buddies: "Sarrazi"});
db.foo.remove();
db.foo.remove({name: "Angie"});
db.foo.remove({buddies: "Sarrazi"});
db.foo.remove();
$ sudo rm -Rf /
$query-Imperator
$query-Imperator
O
{age: {$gt: 10}}{weight: {$gte: 100}}{followers: {$lt: 5}}{shares: {$lte: 40}}{color: {$nin: "red"}}{counter: {$mod: [2, 0]}}{numbers: {$all: [1, 2, 3]}}{sql: {$ne: "nosql"}}{buddies: {$size: 4}}{homies: {$in: "Scooter"}}{brain: {$exists: true}}{name: {$type: 2}}
db.foo.find( )
$query
db.foo.find( )
$query
db.foo.update($query, $update)
{$inc: {counter: 1}}
{$set: {name: "monkey"}}
{$unset: {money: 1}}
{$push: {buddies: ["Homer"]}}
{$pushAll: {numbers: [1, 2]}}
{$addToSet: {drinks: "water"}}
{$pop: {queue: 1}}
{$pop: {queue: -1}}
{$pull: {colors: "red"}}
{$pullAll: {chars: ["a", "b"]}}
$update
$update
dinner in the sky
replicas
S1
S2
S3
Set 1
S1
Set 2
S1
S2
Set 3
Master-Server Slave-ServerS1 S2
sharding
{ { mongod 3
mongod 2
mongod 1
mongod 3
mongod 2
mongod 1
Shard 3Shard 2
mongod 3
app 1 app 2 app 3
mongos 1 mongos 2 mongos 3
Shard 1
config 1
config 2
config 3
Config Servers
mongod 2
mongod 1
indexes
db.foo.ensureIndex({email: 1});
db.foo.ensureIndex({email: 1});
db.foo.ensureIndex({name: -1});
db.foo.ensureIndex({email: 1});
db.foo.ensureIndex({name: -1});
db.foo.ensureIndex({email: 1, password: 1});
db.foo.ensureIndex({email: 1});
db.foo.ensureIndex({name: -1});
db.foo.ensureIndex({email: 1, password: 1});
db.foo.ensureIndex({location: "2d"});
db.foo.ensureIndex({email: 1});
db.foo.ensureIndex({name: -1});
db.foo.ensureIndex({email: 1, password: 1});
db.foo.ensureIndex({location: "2d"});
db.foo.ensureIndex({email: 1}, {unique: true});
gridfs
$ mongofiles listconnected to: 127.0.0.1?
$ mongofiles put foo.jpgconnected to: 127.0.0.1done!
"
$ mongofiles listconnected to: 127.0.0.1foo.jpg12000964?
$ mongofiles get foo.jpgconnected to: 127.0.0.1done write to: foo.jpg
"
map & reduce
$ ./mongo
> db.things.insert( { _id : 1, tags : ['dog', 'cat'] } );
> db.things.insert( { _id : 2, tags : ['cat'] } );
> db.things.insert( { _id : 3, tags : ['mouse', 'cat', 'dog'] } );
> db.things.insert( { _id : 4, tags : [] } );
http://www.flickr.com/photos/lovely-yukari/4388391318/
$ ./mongo
> db.things.insert( { _id : 1, tags : ['dog', 'cat'] } );
> db.things.insert( { _id : 2, tags : ['cat'] } );
> db.things.insert( { _id : 3, tags : ['mouse', 'cat', 'dog'] } );
> db.things.insert( { _id : 4, tags : [] } );
lalala datahttp://www.flickr.com/photos/lovely-yukari/4388391318/
> m = function(){
... this.tags.forEach(
... function(z){
... emit( z , { count : 1 } );
... }
... );
...};
> m = function(){
... this.tags.forEach(
... function(z){
... emit( z , { count : 1 } );
... }
... );
...};
map function
> r = function( key , values ){
... var total = 0;
... for ( var i=0; i<values.length; i++ )
... total += values[i].count;
... return { count : total };
...};
> r = function( key , values ){
... var total = 0;
... for ( var i=0; i<values.length; i++ )
... total += values[i].count;
... return { count : total };
...};
reduce function
> res = db.things.mapReduce(m, r);
> res = db.things.mapReduce(m, r);
do it!
> db[res.result].find()
{"_id" : "cat" , "value" : {"count" : 3}}
{"_id" : "dog" , "value" : {"count" : 2}}
{"_id" : "mouse" , "value" : {"count" : 1}}
> db[res.result].find()
{"_id" : "cat" , "value" : {"count" : 3}}
{"_id" : "dog" , "value" : {"count" : 2}}
{"_id" : "mouse" , "value" : {"count" : 1}}
no rabbit :(
( 25 * ) +*
http://www.flickr.com/photos/frensjan/4011678461/
( 25 * ) +*
http://www.flickr.com/photos/frensjan/4011678461/
8.000.000 operationsper hour
( 25 * ) +*
http://www.flickr.com/photos/frensjan/4011678461/
8.000.000 operationsper hourSECOND
especi
ally fo
r Robe
rto
Cens
ored
Thank you very much for travelling with me!
especi
ally fo
r Robe
rto
Cens
ored