Introduction to MongoDB

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 [codesyntax lang="bash"]
$ mkdir D:\mongodb\data
$ mkdir D:\mongodb\data\db
[/codesyntax]
3. Start mongodb server [codesyntax lang="bash"]
$ cd D:\mongodb\bin
$ mongod.exe --dbpath=D:\mongodb\data\db --rest
[/codesyntax]
4. Web tool : http://localhost:28017/     5. Client tool for mongodb [codesyntax lang="bash"]
$ mongo.exe
[/codesyntax]
6. Create collection called mytestdb and limit the size (in fact we must use client tool) [codesyntax lang="bash"]
> db.createCollection("mytestdb", {capped:true, size:100000})
> show collections
[/codesyntax]
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 [codesyntax lang="ini"]
# modify php.ini
extension=php_mongo.dll
[/codesyntax]


Installation on Linux :

1. Download and install [codesyntax lang="bash"]
$ curl http://downloads.mongodb.org/linux/mongodb-linux-i686-1.6.4.tgz > mongo.tgz
$ tar xzf mongo.tgz
[/codesyntax]
2. Create folder where database located [codesyntax lang="bash"]
$ sudo mkdir -p /data/mongo/db/
$ sudo chown `id -u` /data/mongo/db/
[/codesyntax]
3. Help and Launch [codesyntax lang="bash"]
$ ./mongo/bin/mongod --help
$ ./mongo/bin/mongod --dbpath=/data/mongo/db/
[/codesyntax]
4. Client tool for mongodb [codesyntax lang="bash"]
$ ./mongo/bin/mongo
[/codesyntax]


Operations :

1. Mongo instance and database connection [codesyntax lang="php"]
// 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");
[/codesyntax]
2. Insert [codesyntax lang="php"]
$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'];
[/codesyntax]
3. Query [codesyntax lang="php"]
// 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);
}
[/codesyntax]
4. Create index [codesyntax lang="php"]
$collection->ensureIndex(
    array('email' => 1),
    array('unique' => true, 'background' => true)
);
[/codesyntax]
5. Update [codesyntax lang="php"]
$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
);
[/codesyntax]
6. Store big file (like video) [codesyntax lang="php"]
$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);
[/codesyntax]
7. Remove [codesyntax lang="php"]
// remove document which sessions equ 8
$filter = array('sessions' => 8);
$single = true;
$collection->remove($filter, $single);
[/codesyntax]


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
Posted in MongoDB| Tagged |

Comments are closed.