Minggu, 27 Maret 2011

Using MongoDB and CodeIgniter On Windows


Installing MongoDB


First of all, you need to download MongoDB binary file for windows. You can get these from here. After the download is completed, create a new directory 'C:\data\db'. It is needed as default to store your database. Then, call the command prompt. If you are using Windows 7 as you OS, don't forget to run cmd as administrator. If you don't do this, then the installation won't start.

Type command "fullpath_to_your_mongod.exe_file --install --logpath path_to_log". For example, I have all extracted files on D:\data\installer\mongodb\, so I type "D:\data\installer\mongodb\bin\mongod --install --logpath C:\data\log". I used 'C:\data' directory for the logpath and 'log' as the file name.

At this point, you have successfully installed mongoDB on your machine. Now, you need to get it started. Get the service start by typing 'net start "MongoDB"'. Or alternatively, you can just start the service from services.msc. Find Mongo DB and start the service.

Congratulations ! You have properly installed MongoDB on your computer. To test the database, you can run mongoDB client. Run from command prompt "D:\data\installer\mongodb\mongo". This time, it's okay if you don't run command prompt as administrator. Test any mongodb command there. Here you can get references for MongoDB shell command.

Setting PHP driver

After things with MongoDB are settled, it's time to set MongoDB driver for PHP. Don't worry, this is simple. First, you have to download driver here. Download the correct driver for your environment. At the time this article is created, I downloaded mongo-1.1.4.zip. You will see a lot of file when you extract the file. I'll help you to choose the correct file :
1. VC6 is for Apache (VC9 is for IIS)
2. Thread safe(ts) is for running PHP as an Apache module, non thread safe is for CGI
For me, I'm using apache with php 5.3 installed. Therefore, I'm using 'mongo-1.1.4-php5.3vc6ts'.
Simply copy 'php_mongo.dll' to your 'xampp/php/ext' directory and add 'extension=php_mongo.dll' to your php.ini file. Restart the server and there you go.

Test MongoDB with CodeIgniter

Now, let's take a look on how using MongoDB with CodeIgniter. First, create a controller file and add these lines to your controller's constructor :
// Connect to Mongo
$connection = new Mongo('localhost:27017');

// Select a database
$db = $connection->db_name;

// Select a collection
$books = $db->books;

Now, your controller file should look like this :

class Test extends Controller {

function __construct()
{
parent::Controller();
// Connect to Mongo
$connection = new Mongo('localhost:27017');

// Select a database
$db = $connection->db_name;

// Select a collection
$books = $db->books;
}

function index()
{

}
}

Now, save the file and type 'http://servername/index.php/test' on your browser.
If you see nothing, then it works !
Congratulations ! I'll add more on updating data on my other post.
Enjoy, and thank you guys!