Saturday, May 4, 2013

How to install MongoDB on MAC and integrate it with CodeIgniter

You can easily install MongoDB on you mac using MacPorts


MacPorts

MacPorts distributes build scripts that allow you to easily build packages and their dependencies on your own system. The compilation process can take significant period of time depending on your system’s capabilities and existing dependencies. Issue the following command in the system shell:


port install mongodb


Using MongoDB from Homebrew and MacPorts

The packages installed with Homebrew and MacPorts contain no control scripts or interaction with the system’s process manager.
If you have configured Homebrew and MacPorts correctly, including setting your PATH, the MongoDB applications and utilities will be accessible from the system shell. Start the mongod process in a terminal (for testing or development) or using a process management tool.
mongod
Then open the mongo shell by issuing the following command at the system prompt:
mongo
This will connect to the database running on the localhost interface by default. At the mongo prompt, issue the following two commands to insert a record in the “test” collection of the (default) “test” database and then retrieve that record.
> db.test.save( { a: 1 } ) > db.test.find()
The key part is grabbing the driver binary here: https://github.com/mongodb/mongo-php-driver/downloads
For MAC download your version
  1. Binary for OS X running PHP 5.2

    osx-php52-1.0.11.zip

    Binary for OS X running PHP 5.3

  2. osx-php5.3-1.0.11.zip

Now unzip the downloaded file and copy the mongo.so file to extension folder

/Applications/MAMP/bin/php/php5.3.6/lib/php/extensions/no-debug-non-zts-20090626
Open php.ini file and copy the following code and restart your web server.
extension=mongo.so


Integration MongoDB in Codeigniter

For CodeIgniter there are a few community-built MongoDB libraries you can use. Since I checked almost all of them, use CodeIgniter MongoDB Active Record. Here's a sample code:


// Load MongoDB library 
$this->load->library('mongo_db'); // Query MongoDB: Active document library usage example
$docs = $this->mongo_db->where('age', '33')->get('collection_name_here');

No comments:

Post a Comment