Your SlideShare is downloading. ×
0
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
MongoDB - Introduction
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

MongoDB - Introduction

2,399

Published on

This was a talk given at the first ChennaiGeeks TechMeet at ThoughtWorks' office in Chennai on 16/Oct/2010.

This was a talk given at the first ChennaiGeeks TechMeet at ThoughtWorks' office in Chennai on 16/Oct/2010.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,399
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
38
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide






























  • Transcript

    1. Introducing Vagmi Mudumbai (vagmi@artha42.com)
    2. Who am I?
    3. Who am I? var
person
=
{name:
“Vagmi
Mudumbai”,
type:
”hacker” 













hacks_on:
[“artha42”,”chennaigeeks”], 













uses:
[“mongodb”,”rails”,”sinatra”]}; db.people.save(person);
    4. A bit about NoSQL
    5. Indexing Ad-ho c Quer tor age ies SO NS B M ap/Reduce -place U pdates In Auto Shard Repli ing catio n GridFS What is it about
    6. It makes us a super hero
    7. BSON Storage var
person
=
{name:
“Vagmi
Mudumbai”,
type:
”hacker” 













hacks_on:
[“artha42”,”chennaigeeks”], 













uses:
[“mongodb”,”rails”,”sinatra”]}; db.people.save(person); http://bsonspec.org/ Binary Supports datatypes like • Date • Bindata • Regexs
    8. Querying var
person
=
{name:
“Vagmi
Mudumbai”,
type:
”hacker” 













hacks_on:
[“artha42”,”chennaigeeks”], 













uses:
[“mongodb”,”rails”,”sinatra”], 













age:
30, 













location:
{city:
“chennai”}}; db.people.save(person);
    9. Querying var
person
=
{name:
“Vagmi
Mudumbai”,
type:
”hacker” 













hacks_on:
[“artha42”,”chennaigeeks”], 













uses:
[“mongodb”,”rails”,”sinatra”], 













age:
30, 













location:
{city:
“chennai”}}; db.people.save(person); db.people.find({type:
“hacker”});
    10. Querying var
person
=
{name:
“Vagmi
Mudumbai”,
type:
”hacker” 













hacks_on:
[“artha42”,”chennaigeeks”], 













uses:
[“mongodb”,”rails”,”sinatra”], 













age:
30, 













location:
{city:
“chennai”}}; db.people.save(person); db.people.find({type:
“hacker”}); db.people.find({uses:
“rails”});
//
can
search
within
tags
    11. Querying var
person
=
{name:
“Vagmi
Mudumbai”,
type:
”hacker” 













hacks_on:
[“artha42”,”chennaigeeks”], 













uses:
[“mongodb”,”rails”,”sinatra”], 













age:
30, 













location:
{city:
“chennai”}}; db.people.save(person); db.people.find({type:
“hacker”}); db.people.find({uses:
“rails”});
//
can
search
within
tags db.people.find({age:
{$gt:
25}});
//
can
do
mathematics
    12. Querying var
person
=
{name:
“Vagmi
Mudumbai”,
type:
”hacker” 













hacks_on:
[“artha42”,”chennaigeeks”], 













uses:
[“mongodb”,”rails”,”sinatra”], 













age:
30, 













location:
{city:
“chennai”}}; db.people.save(person); db.people.find({type:
“hacker”}); db.people.find({uses:
“rails”});
//
can
search
within
tags db.people.find({age:
{$gt:
25}});
//
can
do
mathematics db.people.find({“location.city”:”chennai”});
//can
search
 












































//
within
docs
    13. Querying var
person
=
{name:
“Vagmi
Mudumbai”,
type:
”hacker” 













hacks_on:
[“artha42”,”chennaigeeks”], 













uses:
[“mongodb”,”rails”,”sinatra”], 













age:
30, 













location:
{city:
“chennai”}}; db.people.save(person); db.people.find({type:
“hacker”}); db.people.find({uses:
“rails”});
//
can
search
within
tags db.people.find({age:
{$gt:
25}});
//
can
do
mathematics db.people.find({“location.city”:”chennai”});
//can
search
 












































//
within
docs db.people.find().sort({age:1}).skip(10).limit(10)
//pagination
    14. Indexing var
person
=
{name:
“Vagmi
Mudumbai”,
type:
”hacker” 













hacks_on:
[“artha42”,”chennaigeeks”], 













uses:
[“mongodb”,”rails”,”sinatra”]}; db.people.save(person); db.people.ensureIndex({“type”:1}); db.people.ensureIndex({“uses”:1}); db.people.getIndexes();
//
returns
a
list
of
indexes No different from a standard database Can index on multiple fields Can index into sub documents and arrays _id is automatically indexed
    15. Updates var
person
=
{name:
“Vagmi
Mudumbai”,
type:
”hacker” 













hacks_on:
[“artha42”,”chennaigeeks”], 













uses:
[“mongodb”,”rails”,”sinatra”], 













age:
30, 













location:
{city:
“chennai”}}; db.people.save(person);
    16. Updates var
person
=
{name:
“Vagmi
Mudumbai”,
type:
”hacker” 













hacks_on:
[“artha42”,”chennaigeeks”], 













uses:
[“mongodb”,”rails”,”sinatra”], 













age:
30, 













location:
{city:
“chennai”}}; db.people.save(person); var
p
=
db.people.findOne({type:
“hacker”}); p.age
=
29;
 db.people.save(p);
//updates
the
above
record
    17. Updates var
person
=
{name:
“Vagmi
Mudumbai”,
type:
”hacker” 













hacks_on:
[“artha42”,”chennaigeeks”], 













uses:
[“mongodb”,”rails”,”sinatra”], 













age:
30, 













location:
{city:
“chennai”}}; db.people.save(person); var
p
=
db.people.findOne({type:
“hacker”}); p.age
=
29;
 db.people.save(p);
//updates
the
above
record var
p
=
db.people.findOne({type:
“hacker”}); db.people.update({_id:
p._id},{$inc:{age:1}});
    18. Updates var
person
=
{name:
“Vagmi
Mudumbai”,
type:
”hacker” 













hacks_on:
[“artha42”,”chennaigeeks”], 













uses:
[“mongodb”,”rails”,”sinatra”], 













age:
30, 













location:
{city:
“chennai”}}; db.people.save(person); var
p
=
db.people.findOne({type:
“hacker”}); p.age
=
29;
 db.people.save(p);
//updates
the
above
record var
p
=
db.people.findOne({type:
“hacker”}); db.people.update({_id:
p._id},{$inc:{age:1}}); var
p
=
db.people.findOne({type:
“hacker”}); db.people.update({_id:
p._id}, 
















{$inc:{age:1},$push:{uses:
“vim”}});
    19. Map/Reduce {
url:
“/blog/something”, 

user_id:
ObjectID(‘xxxxxx’), 

date:
“Sat
Oct
16
2010
06:30:00
GMT+0530
(IST)”
} Source: http://cookbook.mongodb.org/patterns/unique_items_map_reduce/
    20. Map/Reduce {
url:
“/blog/something”, 

user_id:
ObjectID(‘xxxxxx’), 

date:
“Sat
Oct
16
2010
06:30:00
GMT+0530
(IST)”
} map
=
function()
{ day
=
Date.UTC(this.date.getFullYear(), 














this.date.getMonth(),this.date.getDate); emit({day:
day,
user_id:
this.user_id},{count:
1}); } Source: http://cookbook.mongodb.org/patterns/unique_items_map_reduce/
    21. Map/Reduce {
url:
“/blog/something”, 

user_id:
ObjectID(‘xxxxxx’), 

date:
“Sat
Oct
16
2010
06:30:00
GMT+0530
(IST)”
} map
=
function()
{ day
=
Date.UTC(this.date.getFullYear(), 














this.date.getMonth(),this.date.getDate); emit({day:
day,
user_id:
this.user_id},{count:
1}); } reduce
=
function(key,values){ var
count=0; values.forEach(function(v){ count+=v; } return
{count:
count} } Source: http://cookbook.mongodb.org/patterns/unique_items_map_reduce/
    22. Map/Reduce {
url:
“/blog/something”, 

user_id:
ObjectID(‘xxxxxx’), 

date:
“Sat
Oct
16
2010
06:30:00
GMT+0530
(IST)”
} map
=
function()
{ day
=
Date.UTC(this.date.getFullYear(), 














this.date.getMonth(),this.date.getDate); emit({day:
day,
user_id:
this.user_id},{count:
1}); } reduce
=
function(key,values){ var
count=0; values.forEach(function(v){ count+=v; } return
{count:
count} } db.pageviews.mapReduce(map,reduce,{out:
pageview_results}); Source: http://cookbook.mongodb.org/patterns/unique_items_map_reduce/
    23. Map/Reduce (contd) {
_id:
{day:
“Sat
Oct
16
2010
00:00:00Z”,
 







user_id:
ObjectId(xxx)}, 

count:
20} Source: http://cookbook.mongodb.org/patterns/unique_items_map_reduce/
    24. Map/Reduce (contd) {
_id:
{day:
“Sat
Oct
16
2010
00:00:00Z”,
 







user_id:
ObjectId(xxx)}, 

count:
20} map
=
function()
{ emit(this,{count:
1}); } Source: http://cookbook.mongodb.org/patterns/unique_items_map_reduce/
    25. Map/Reduce (contd) {
_id:
{day:
“Sat
Oct
16
2010
00:00:00Z”,
 







user_id:
ObjectId(xxx)}, 

count:
20} map
=
function()
{ emit(this,{count:
1}); } reduce
=
function(key,values){ var
count=0; values.forEach(function(v){ count+=v; } return
{count:
count} } Source: http://cookbook.mongodb.org/patterns/unique_items_map_reduce/
    26. Map/Reduce (contd) {
_id:
{day:
“Sat
Oct
16
2010
00:00:00Z”,
 







user_id:
ObjectId(xxx)}, 

count:
20} map
=
function()
{ emit(this,{count:
1}); } reduce
=
function(key,values){ var
count=0; values.forEach(function(v){ count+=v; } return
{count:
count} } db.pageview_results.mapReduce(map,reduce, 





























{out:
pageview_results_unique}); Source: http://cookbook.mongodb.org/patterns/unique_items_map_reduce/
    27. GridFS (in Ruby) Source: http://cookbook.mongodb.org/patterns/unique_items_map_reduce/
    28. GridFS (in Ruby) @db
=
Mongo::Connection.new.db(“chennaigeeks”) @fs
=
GridFileSystem.new(@db) Source: http://cookbook.mongodb.org/patterns/unique_items_map_reduce/
    29. GridFS (in Ruby) @db
=
Mongo::Connection.new.db(“chennaigeeks”) @fs
=
GridFileSystem.new(@db) image
=
File.open(“me.png”) @fs.open(“me.png”,’w’)
do
|f| f.write
image end Source: http://cookbook.mongodb.org/patterns/unique_items_map_reduce/
    30. GridFS (in Ruby) @db
=
Mongo::Connection.new.db(“chennaigeeks”) @fs
=
GridFileSystem.new(@db) image
=
File.open(“me.png”) @fs.open(“me.png”,’w’)
do
|f| f.write
image end img
=
@fs.open(“me.png”,”r”)
{
|f|
f.read
} Source: http://cookbook.mongodb.org/patterns/unique_items_map_reduce/
    31. Replication (Master/Slave) db@master
/opt/mongo/bin$
./mongod
‐‐master db@slave
/opt/mongo/bin$
./mongod
‐‐slave
‐‐source
master
    32. Replication (Replica Sets) db@node1
/opt/mongo/bin$
./mongod
‐‐replSet
myRS db@node2
/opt/mongo/bin$
./mongod
‐‐replSet
myRS db@node3
/opt/mongo/bin$
./mongod
‐‐replSet
myRS db@dev
/opt/mongo/bin$
./mongo
node1 MongoDB
shell
version:
1.6.0 connecting
to:
node1/test >
config
=
{_id:
“myRS”,
members
:
[ {_id:
0,
host:
“node1”}, {_id:
0,
host:
“node2”}, {_id:
0,
host:
“node3”}]} >
rs.initiate(config); >
rs.status()
    33. Auto Sharding db@node1
/opt/mongo/bin$
./mongod
‐‐shardsvr db@node2
/opt/mongo/bin$
./mongod
‐‐shardsvr db@node3
/opt/mongo/bin$
./mongod
‐‐configsvr db@appserver
/opt/mongo/bin$
./mongos
‐‐configdb
node3
    34. Auto Sharding (contd) db@appserver
/opt/mongo/bin$
./mongo MongoDB
shell
version:
1.6.0 connecting
to:
test >
use
admin switched
to
db
admin >
db.runCommand({addShard
:
“node1”}); {
“shardadded”:
“shard0000”,
”ok”:
1} >
db.runCommand({addShard
:
“node2”}); {
“shardadded”:
“shard0001”,
”ok”:
1} >
db.runCommand(enableSharding
:
“mydb”) {“ok”
:
1} >
db.runCommand({
shardCollection
:
“mydb.people”,
 

















key
:
{name
:
1}
}); {“ok”
:
1
}
    35. Any Questions?
    36. vagmi@artha42.com http://blog.chennaigeeks.com/

    ×