Installation on Windows :
1. Download and install
http://fastdl.mongodb.org/win32/mongodb-win32-i386-1.6.5.zip
unzip mongodb-win32-i386-1.6.5.zip to D:\mongodb
2. Create folder where database located
$ mkdir D:\mongodb\data
$ mkdir D:\mongodb\data\db
3. Start mongodb server
$ cd D:\mongodb\bin
$ mongod.exe --dbpath=D:\mongodb\data\db --rest
4. Web tool : http://localhost:28017/

5. Client tool for mongodb
6. Create collection called mytestdb and limit the size (in fact we must use client tool)
> db.createCollection("mytestdb", {capped:true, size:100000})
> show collections
7. Driver for windows
https://github.com/mongodb/mongo-php-driver/downloads
https://github.com/downloads/mongodb/mongo-php-driver/mongo-1.1.3.zip
unzip mongo-1.1.3.zip
put mongo-1.1.3\mongo-1.1.3-php5.3vc6ts\php_mongo.dll to D:\xampp\php\ext\php_mongo.dll
# modify php.ini
extension=php_mongo.dll
Installation on Linux :
1. Download and install
$ curl http://downloads.mongodb.org/linux/mongodb-linux-i686-1.6.4.tgz > mongo.tgz
$ tar xzf mongo.tgz
2. Create folder where database located
$ sudo mkdir -p /data/mongo/db/
$ sudo chown `id -u` /data/mongo/db/
3. Help and Launch
$ ./mongo/bin/mongod --help
$ ./mongo/bin/mongod --dbpath=/data/mongo/db/
4. Client tool for mongodb
Operations :
1. Mongo instance and database connection
// connect to database server
$mongo = new mongo("192.168.0.132:27017");
// select database to handle
$db = $mongo->selectDB("mytest");
// select collection (just like table in mysql)
$collection = $db->selectCollection("cartoons");
2. Insert
$myinfo = array(
'name' => 'Kim',
'email' => 'xqpmjh@gmail.com',
'sessions' => 0
,
);
$safe_insert = true;
// $person is passed by reference
$collection->insert($person, $safe_insert);
echo $myid = $myinfo['_id'];
3. Query
// find the person by both email and sessions exists
$filter = array(
'email' => 'xqpmjh@gmail.com',
'sessions' => array('$exists' => true),
);
// order by sessions desc
$cursor = $collection->find($filter)->sort(
array('sessions' => -1
)
);
// show total docs
echo $total = $cursor->count();
// skip the first one and limit 3 docs
$cursor->limit(3
)->skip(1
);
foreach ($cursor as $user) {
var_dump($user);
}
4. Create index
$collection->ensureIndex(
array('email' => 1
),
array('unique' => true, 'background' => true)
);
5. Update
$filter = array('email' => 'xqpmjh@gmail.com');
$new_document = array(
// increase sessions by one
'$inc' => array('sessions' => 1),
// set new name
'$set' => array(
'name' => 'kimho',
),
// unset second address
'$unset' => array('address.1' => 1
),
);
// for duplicated records
$options['multiple'] = false;
$collection->update(
$filter,
$new_document,
$options
);
6. Store big file (like video)
$grid = $db->getGridFS();
$metadata = array(
"filename" => "foo.avi",
"comment" => "wooooooo!",
"permissions" => array(
"kim" => "write",
"everybody" => "read",
)
);
$grid = $db->getGridFS();
$grid->storeFile("/data/video/avatar.avi", $metadata);
7. Remove
// remove document which sessions equ 8
$filter = array('sessions' => 8
);
$single = true;
$collection->remove($filter, $single);
References :
http://us.php.net/manual/en/book.mongo.php
http://www.mongodb.org/display/DOCS/Home
http://www.phpclasses.org/blog/post/118-Developing-scalable-PHP-applications-using-MongoDB.html