how to integrate mongodb library in codeigniter for database connection with mongodb

user3572540 picture user3572540 · Aug 19, 2014 · Viewed 10.8k times · Source

I am creating application with codeigniter and mongodb database. As codeigniter doesn't contain mongodbs driver, I have used built in library to connect with mongodb. I have used this link to create mongodb configration file and Mongo_db library. The configration file is like application\config\mongodb:

<?php
$config['default']['mongo_hostbase'] = 'localhost:27017';
$config['default']['mongo_database'] = 'test';
$config['default']['mongo_username'] = '';
$config['default']['mongo_password'] = '';
$config['default']['mongo_persist']  = FALSE;
$config['default']['mongo_persist_key']  = 'ci_persist';
$config['default']['mongo_replica_set']  = FALSE;
$config['default']['mongo_query_safety'] = 'safe';
$config['default']['mongo_suppress_connect_error'] = FALSE;
$config['default']['mongo_host_db_flag']   = FALSE; 

?> 

and Mongo_db library is like:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mongo_db
{
  public function __construct()
    {

                //Check mongodb is installed in your server otherwise display an error
        if ( ! class_exists('Mongo'))
        {
            $this->_show_error('The MongoDB PECL extension has not been installed or enabled', 500);
        }

                //get instance of CI class
        if (function_exists('get_instance'))
        {
            $this->_ci = get_instance();
        }

        else
        {
            $this->_ci = NULL;
        }   

                //load the config file which we have created in 'config' directory
        $this->_ci->load->config('mongodb');


            $config='default';
                // Fetch Mongo server and database configuration from config file which we have created in 'config' directory
                $config_data = $this->_ci->config->item($config);  

            try{
                   //connect to the mongodb server
           $this->mb = new Mongo('mongodb://'.$config_data['mongo_hostbase']);
                   //select the mongodb database 
                   $this->db=$this->mb->selectDB($config_data['mongo_database']);
        } 
        catch (MongoConnectionException $exception)
        {
                     //if mongodb is not connect, then display the error
            show_error('Unable to connect to Database', 500);           
        }


}
?>

When I run the application it give me an error like An Error Was Encountered Your application/config/mongodb.php file does not appear to contain a valid configuration array.

can anybody tell How to resolve this error and setup connection with mongodb?

Answer

Godzilla picture Godzilla · Mar 27, 2015

You might want to try this repo: https://github.com/vesparny/cimongo-codeigniter-mongodb-library, seems pretty easy to use.

A full list of PHP libraries can be found here: http://docs.mongodb.org/ecosystem/drivers/php-libraries/