I just wanted to try mongodb with php for an idea that I had with my friend. So I just started it with basic setup example. MongoDB is cool and very simple to use. If you are using Windows Machine to setup Mongo, here is the steps.
Here is the installation steps for Windows:
- Install XAMPP
- You should have PHP 5.3 and above. Check it through (localhost/xampp/phpinfo.php)
- Download mongodb from their official site
- Extract the archive. Open command prompt and goto the bin folder inside that.
- Type “mkdir c:\mongodb\db”
- Type “mongod.exe –dbpath=C:\mongodb\db”
- Download windows driver for mongodb from GitHub page. Download the mongo-1.1.4.zip build that works for me
- Extract the archive. And find the build suitable to your computer. Copy the php_mongo.dll and paste it inside C:\xampp\php\ext folder
- Open php.ini file from c:\xampp\php
- Add “extension=php_mongo.dll” line to the file
- Save it and close. Restart apache after that
- Try this example code from php.net tutorial
Thats it!
// select a collection (analogous to a relational database’s table)
$collection = $db->pics;
// add a record
$obj = array( “title” => “RajiniKanth”, “author” => “karthi” );
$collection->insert($obj);
// add another record, with a different “shape”
$obj = array( “title” => “Trisha”, “online” => true );
$collection->insert($obj);
// find everything in the collection
$cursor = $collection->find();
// iterate through the results
foreach ($cursor as $obj) {
echo $obj[“title”] . “\n”;
}
Thank you. I was looking for this only. I had issues with the latest windows driver for mongo db.