MongoDB
Rawin Viruchpintu
Creative Technology Leader
Spriiing Telecom Co.,Ltd.
July 20, 2012
Outline
• MongoDB
• Installation & start
• Using Mongo
• Document-oriented
• Dynamic queries
• Full dynamic index support
• Efficient binary large-object storage
• Built for speed
• Replication and Auto-failover
Installation & start
• Download
http://www.mongodb.org/downloads
• Unzip into a folder
for example /some/path/mongodb
• Run
$bin/mongod --dbpath=/some/path/mongodb
Installation via Rails
• $ gem install mongo
Successfully installed mongo-1.6.4
1 gem installed
Installing ri documentation for mongo-1.6.4...
Installing RDoc documentation for mongo-1.6.4...
Database structure
• Separate DBs
• Organized into Collections
Top-level key -> Document
• Collections
– Group things into logical classes
– Indexable by one or more keys
– Schema-free!
Database structure
• Document
– Always contains key_id
– Creating Relationships:
subdocument, shared key, or DBRef
– Native storage and transfer: BSON
• BSON
– is a binary encoded serialization of JSON-like
documents.
http://bsonspec.org/
Using Mongo
D:mongodbbin>mongo
MongoDB shell version: 2.0.6
connecting to: test
> show dbs
local (empty)
test (empty)
> use test
switched to db test
> db.test.find()
> db.test.save({ name:'windy' , skills: 'blackberry, perl, php' })
> db.test.find()
{ "_id" : ObjectId("5008eb49839a10d95a068a01"), "name" : "windy", "skills" : "bl
ackberry, perl, php" }
Using Mongo
>use things
switched to db things
>for ( var i=1; i<10; i++) db.things.save( {x: 'No.'+i, counter: i } )
>db.things.find()
{ "_id" : ObjectId("5008f81d839a10d95a068a02"), "x" : "No.1", "counter" : 1 }
{ "_id" : ObjectId("5008f81d839a10d95a068a03"), "x" : "No.2", "counter" : 2 }
{ "_id" : ObjectId("5008f81d839a10d95a068a04"), "x" : "No.3", "counter" : 3 }
{ "_id" : ObjectId("5008f81d839a10d95a068a05"), "x" : "No.4", "counter" : 4 }
{ "_id" : ObjectId("5008f81d839a10d95a068a06"), "x" : "No.5", "counter" : 5 }
{ "_id" : ObjectId("5008f81d839a10d95a068a07"), "x" : "No.6", "counter" : 6 }
{ "_id" : ObjectId("5008f81d839a10d95a068a08"), "x" : "No.7", "counter" : 7 }
{ "_id" : ObjectId("5008f81d839a10d95a068a09"), "x" : "No.8", "counter" : 8 }
{ "_id" : ObjectId("5008f81d839a10d95a068a0a"), "x" : "No.9", "counter" : 9 }
Using Mongo
> db.things.find({x: 'No.4'})
{ "_id" : ObjectId("5008f81d839a10d95a068a05"), "x" : "No.4", "counter" : 4 }
> db.things.find({x: 'No.33'})
>
> db.things.find( { counter : { $gt : 5, $lte : 8} })
{ "_id" : ObjectId("5008f81d839a10d95a068a07"), "x" : "No.6", "counter" : 6 }
{ "_id" : ObjectId("5008f81d839a10d95a068a08"), "x" : "No.7", "counter" : 7 }
{ "_id" : ObjectId("5008f81d839a10d95a068a09"), "x" : "No.8", "counter" : 8 }
Using Mongo
> show dbs
local (empty)
things 0.078125GB
> use foo
switched to db foo
> db.foo.save( { name: 'windy', age: 18 } )
> db.foo.find()
{ "_id" : ObjectId("5008fa33839a10d95a068a0b"), "name" : "windy", "age" : 18 }
> db.foo.update( {name: 'windy' }, { $set : { age : 21 } } )
> db.foo.find()
{ "_id" : ObjectId("5008fa33839a10d95a068a0b"), "name" : "windy", "age" : 21 }
Using Mongo
> db.foo.findOne( { name: 'windy'} )
{
"_id" : ObjectId("5008fa33839a10d95a068a0b"),
"name" : "windy",
"age" : 21
}
Using Mongo
> use teams
switched to db teams
> db.teams.save({ team: 'A' , member : [ 'Kai'] })
> db.teams.find()
{
"_id" : ObjectId("5008fc13839a10d95a068a0c"),
"team" : "A",
"member" : [ "Kai” ]
}
> db.teams.update({ team: 'A' },{ $push : { member : 'Windy' } })
> db.teams.findOne()
{
"_id" : ObjectId("5008fc13839a10d95a068a0c"),
"member" : [ "Kai",
"Windy" ],
“team" : "A"
}
Using Mongo
> use simple
switched to db simple
> db.members.save( { name:"Windy", skill: ["java", "php"] } )
> db.teams.save({ team: 'A' })
Members Teams
Windy Project A
project_a =
Using Mongo
> windy = db.members.findOne( {name: "Windy"} )
{
"_id" : ObjectId("50090510839a10d95a068a0f"),
"name" : "Windy",
"skill" : [
"java",
"php"
]
}
> project_a = db.teams.findOne( { team : "A" } )
{ "_id" : ObjectId("50090533839a10d95a068a10"), "team" : "A" }
Project A
Windywindy =
Using Mongo
> project_a.members = []
[ ]
> project_a.members.push (new DBRef ('members', windy._id) )
1
> db.teams.save(project_a)
> project_a
{
"_id" : ObjectId("50090533839a10d95a068a10"),
"team" : "A",
"members" : [
{
"$ref" : "members",
"$id" : ObjectId("50090510839a10d95a068a0f")
}
]
}
Project A
windy = DBRef( )
Teams
Using Mongo
> project_a.members
[ { "$ref" : "members", "$id" : ObjectId("50090510839a10d95a068a0f") } ]
> project_a.members[0]
{ "$ref" : "members", "$id" : ObjectId("50090510839a10d95a068a0f") }
> project_a.members[0].fetch()
{
"_id" : ObjectId("50090510839a10d95a068a0f"),
"name" : "Windy",
"skill" : [
"java",
"php"
]
}
THANK YOU

MongoDB

  • 1.
    MongoDB Rawin Viruchpintu Creative TechnologyLeader Spriiing Telecom Co.,Ltd. July 20, 2012
  • 2.
    Outline • MongoDB • Installation& start • Using Mongo
  • 3.
    • Document-oriented • Dynamicqueries • Full dynamic index support • Efficient binary large-object storage • Built for speed • Replication and Auto-failover
  • 4.
    Installation & start •Download http://www.mongodb.org/downloads • Unzip into a folder for example /some/path/mongodb • Run $bin/mongod --dbpath=/some/path/mongodb
  • 5.
    Installation via Rails •$ gem install mongo Successfully installed mongo-1.6.4 1 gem installed Installing ri documentation for mongo-1.6.4... Installing RDoc documentation for mongo-1.6.4...
  • 6.
    Database structure • SeparateDBs • Organized into Collections Top-level key -> Document • Collections – Group things into logical classes – Indexable by one or more keys – Schema-free!
  • 7.
    Database structure • Document –Always contains key_id – Creating Relationships: subdocument, shared key, or DBRef – Native storage and transfer: BSON • BSON – is a binary encoded serialization of JSON-like documents. http://bsonspec.org/
  • 9.
    Using Mongo D:mongodbbin>mongo MongoDB shellversion: 2.0.6 connecting to: test > show dbs local (empty) test (empty) > use test switched to db test > db.test.find() > db.test.save({ name:'windy' , skills: 'blackberry, perl, php' }) > db.test.find() { "_id" : ObjectId("5008eb49839a10d95a068a01"), "name" : "windy", "skills" : "bl ackberry, perl, php" }
  • 10.
    Using Mongo >use things switchedto db things >for ( var i=1; i<10; i++) db.things.save( {x: 'No.'+i, counter: i } ) >db.things.find() { "_id" : ObjectId("5008f81d839a10d95a068a02"), "x" : "No.1", "counter" : 1 } { "_id" : ObjectId("5008f81d839a10d95a068a03"), "x" : "No.2", "counter" : 2 } { "_id" : ObjectId("5008f81d839a10d95a068a04"), "x" : "No.3", "counter" : 3 } { "_id" : ObjectId("5008f81d839a10d95a068a05"), "x" : "No.4", "counter" : 4 } { "_id" : ObjectId("5008f81d839a10d95a068a06"), "x" : "No.5", "counter" : 5 } { "_id" : ObjectId("5008f81d839a10d95a068a07"), "x" : "No.6", "counter" : 6 } { "_id" : ObjectId("5008f81d839a10d95a068a08"), "x" : "No.7", "counter" : 7 } { "_id" : ObjectId("5008f81d839a10d95a068a09"), "x" : "No.8", "counter" : 8 } { "_id" : ObjectId("5008f81d839a10d95a068a0a"), "x" : "No.9", "counter" : 9 }
  • 11.
    Using Mongo > db.things.find({x:'No.4'}) { "_id" : ObjectId("5008f81d839a10d95a068a05"), "x" : "No.4", "counter" : 4 } > db.things.find({x: 'No.33'}) > > db.things.find( { counter : { $gt : 5, $lte : 8} }) { "_id" : ObjectId("5008f81d839a10d95a068a07"), "x" : "No.6", "counter" : 6 } { "_id" : ObjectId("5008f81d839a10d95a068a08"), "x" : "No.7", "counter" : 7 } { "_id" : ObjectId("5008f81d839a10d95a068a09"), "x" : "No.8", "counter" : 8 }
  • 12.
    Using Mongo > showdbs local (empty) things 0.078125GB > use foo switched to db foo > db.foo.save( { name: 'windy', age: 18 } ) > db.foo.find() { "_id" : ObjectId("5008fa33839a10d95a068a0b"), "name" : "windy", "age" : 18 } > db.foo.update( {name: 'windy' }, { $set : { age : 21 } } ) > db.foo.find() { "_id" : ObjectId("5008fa33839a10d95a068a0b"), "name" : "windy", "age" : 21 }
  • 13.
    Using Mongo > db.foo.findOne({ name: 'windy'} ) { "_id" : ObjectId("5008fa33839a10d95a068a0b"), "name" : "windy", "age" : 21 }
  • 14.
    Using Mongo > useteams switched to db teams > db.teams.save({ team: 'A' , member : [ 'Kai'] }) > db.teams.find() { "_id" : ObjectId("5008fc13839a10d95a068a0c"), "team" : "A", "member" : [ "Kai” ] } > db.teams.update({ team: 'A' },{ $push : { member : 'Windy' } }) > db.teams.findOne() { "_id" : ObjectId("5008fc13839a10d95a068a0c"), "member" : [ "Kai", "Windy" ], “team" : "A" }
  • 15.
    Using Mongo > usesimple switched to db simple > db.members.save( { name:"Windy", skill: ["java", "php"] } ) > db.teams.save({ team: 'A' }) Members Teams Windy Project A
  • 16.
    project_a = Using Mongo >windy = db.members.findOne( {name: "Windy"} ) { "_id" : ObjectId("50090510839a10d95a068a0f"), "name" : "Windy", "skill" : [ "java", "php" ] } > project_a = db.teams.findOne( { team : "A" } ) { "_id" : ObjectId("50090533839a10d95a068a10"), "team" : "A" } Project A Windywindy =
  • 17.
    Using Mongo > project_a.members= [] [ ] > project_a.members.push (new DBRef ('members', windy._id) ) 1 > db.teams.save(project_a) > project_a { "_id" : ObjectId("50090533839a10d95a068a10"), "team" : "A", "members" : [ { "$ref" : "members", "$id" : ObjectId("50090510839a10d95a068a0f") } ] } Project A windy = DBRef( ) Teams
  • 18.
    Using Mongo > project_a.members [{ "$ref" : "members", "$id" : ObjectId("50090510839a10d95a068a0f") } ] > project_a.members[0] { "$ref" : "members", "$id" : ObjectId("50090510839a10d95a068a0f") } > project_a.members[0].fetch() { "_id" : ObjectId("50090510839a10d95a068a0f"), "name" : "Windy", "skill" : [ "java", "php" ] }
  • 19.