Академический Документы
Профессиональный Документы
Культура Документы
MongoDB's Goal:
be the best database for (most)
web apps
...not the best DB for everything.
What do Web Apps Want?
Performance
Scalability
Availability
What do developers want?
Easier development,
Less server maintenance (at 3am)
Great support
Goals
Easy-to-use
Fast
Always available
Easy to scale
Installing a New Database
1. Download the archive from
http://www.mongodb.org
2. Make a directory for database files
3. bin/mongod --dbpath ~/dbdir
4. sudo pecl install mongo
Connecting
$connection = new Mongo();
Database
$connection = new Mongo();
$db = $connection->foo;
Collection
$connection = new Mongo();
$db = $connection->foo;
$collection = $db->bar;
A:
echo $x['_id'];
2fe3e4d892aa73234c910bed
|------||----||--||----|
ts mac pid inc
In Mongo:
$db->encounters->findOne()
Getting a Bunch of Encounters
$cursor = $db->encounters->find();
$total = $cursor->count();
Kingdom of Loathing
{
"description" : "You munch the sausage.
Sausage muncher.",
"type" : "food",
"effect" : {
"adventures" : [3, 10]
}
}
Kingdom of Loathing
{
"title" : "Ninja Snowman Weaponmaster",
"type" : "fight",
"stats" : {
"hit points" : [200, 230],
"offense" : [20, 30]
}
"attacks" : [
{"desc" : "He strangles you mercilessly with a length of
chain...", damage : [30, 40]},
{"desc" : "He backflips over your head clocking you
with...", damage : [40, 50]}
]
"areas" : ["Mt. McLargeHuge"]
}
Kingdom of Loathing - User
{
"username" : "kristina",
"level" : 9,
}
I acquire a pet
$db->users->update(
array('username' => 'kristina'),
array('$set' => array('pet' => array(
'name' => 'Trot',
'species' => 'Mosquito',
'weight' => 20))));
Kingdom of Loathing
{
"username" : "kristina",
"level" : 9,
"pet" : {
"name" : "Trot",
"species" : "Mosquito",
"weight" : 20
}
}
Kingdom of Loathing
$users->find(array("pet.weight" => 20));
$users->find(array("pet.weight" =>
array('$gt' => 20));
$users->find(array("pet.weight" =>
array('$gte' => 20));
$users->find(array("pet.weight" =>
array('$ne' => 20));
Updates
In SQL:
UPDATE foo SET bar="baz" WHERE bar="bat"
In Mongo:
$db->foo->update(
array("bar" => "bat"),
array('$set' =>
array("bar" => "baz")));
Analytics
{
"url" : "www.example.com",
"pageviews" : 0
}
Analytics - Increment Pageviews
$page = $analytics->findOne(array(
"url" => "www.example.com"));
if ($page != NULL) {
$page['pageviews']++;
$analytics->save($page);
}
else {
$analytics->insert(array(
"url" => "www.example.com",
"pageviews" => 1));
}
...that's 1 round trip + 1 update or insert.
Analytics - Upsert
$analytics->update(
array("url" => "www.example.com"),
array('$inc' => array("pageviews" => 1)),
array("upsert" => true));
$c->ensureIndex(
array("x" => 1, "y" => -1));
Indexes
array("foo" =>
array("bar" =>
array("baz" => 1)
)
);
$c->ensureIndex(
array("foo.bar.baz" => 1));
Goals
Easy-to-use
Fast
Always available
Easy to scale
Master-Slave
master
slave
Replica Pairs
slave
Replica Pairs
master
Replica Pairs
slave
master
Goals
Easy-to-use
Fast
Always available
Easy to scale
Scaling with Auto-Sharding
April - July 2009
Configuration
Server #3, lockdown.
Split the data,
migrate half to the
new shard.
Configuration
Routers
"Yeah, the RDBMS went down
at 2am. There wasn't any need
to wake the DBA. She can fix it
later today."
Insert this.
Insert this.
Okay
Fred
Consistency
x 1,000,000
Fred
Fail Whale
Consistency
Okay
Fred
Consistency
Okay
Fred
Bob
Consistency
One sec
Fred
Consistency
Wait a sec, Bob
Bob
Consistency
Bob
Bob
J J J
chunks
J J J
J J J
_id : J files
Storing Files - GridFS
$grid = $db->getGridFS();
$grid->insert($filename,
array("permissions" => 644,
"comment" => "vacation pics");
$file->write($filename);
or
echo $file->getBytes();
Mongo Knows JavaScript
$ ./mongo
MongoDB shell version: 1.3.2-
url: test
connecting to: test
type "help" for help
> x = 123+1
124
> db.foo.insert({name : "Fred"})
>
$where
$c->findOne(array('$where' =>
'this.y == (this.x + this.z)'));
Will work:
array("x" => 1, "y" => 4, "z" => 3)
array("x" => "hi", "y" => "hibye", "z" => "bye")
Won’t work:
array("x" => 1, "y" => 1)
JavaScript Functions
$db->execute("function() { return 'hello'; }")
hello
$func = <<<JSFUNC
function(x) {
return "hello "+ x + "!";
}
JSFUNC;
$db->execute($func, array("joe"))
hello, joe!
"Stored Procedures"
$db->system->js->insert(array(
"_id" => "x",
"value" => 3));
$db->system->js->insert(array(
"_id" => "y",
"value" => 4));
$db->execute("return x+y");
Wrap Up: PHP Frameworks
CakePHP
Drupal
Kohana
Lithium
Symfony
irc.freenode.net#mongodb
www.mongodb.org