6
votes

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?

4
mongo_username, and mongo_password are blankecorvo
Just testing your code and found you were missing a } at the end of your library.jbrahy

4 Answers

3
votes

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/

1
votes

Try Replacing

$this->mb = new Mongo('mongodb://'.$config_data['mongo_hostbase']);

with these two lines in your library/mongo_db.php

$url = 'mongodb://'.$config_data['mongo_username'].':'.$config_data['mongo_password'].'@'.$config_data['mongo_hostbase'].'/'.$config_data['mongo_database'];
$this->mb = new MongoClient($url);

Did the job for me :)

0
votes

If you want to use mongoDB in your codeIgniter project as a database then this post may be helpful for you. There are some simple steps to configure mongoDB in your project.

  1. Install PECL pakcage.

For this you can take help from official PHP site – http://php.net/manual/en/install.pecl.php

  1. Download CodeIgniter library.

You have to download a library which helps to create connection with mongoDB enter link description here

  1. Install mongoDB.

You have to install mongoDB database in your system. According to your opearting system download installable file from https://docs.mongodb.com/manual/installation and install.

  1. Set up library.

Extract library and you will get a config file which you need to place in application/config folder and a library file which shoulb be placed in application/libraries folder. In mongoDB config file enter your credentials like

$config['mongo_db']['default']['no_auth'] = FALSE;
$config['mongo_db']['default']['hostname'] = 'localhost';
$config['mongo_db']['default']['port'] = '27017';
$config['mongo_db']['default']['username'] = 'aniket';
$config['mongo_db']['default']['password'] = 'password';
$config['mongo_db']['default']['database'] = 'admin';
$config['mongo_db']['default']['db_debug'] = TRUE;
$config['mongo_db']['default']['return_as'] = 'array';
$config['mongo_db']['default']['write_concerns'] = (int)1;
$config['mongo_db']['default']['journal'] = TRUE;
$config['mongo_db']['default']['read_preference'] = NULL;
$config['mongo_db']['default']['read_preference_tags'] = NULL;
  1. Load library.

As a normal library whenever you want to use mongoDB in your project, you will need to load mongoDB library. For this you can use this code.

$this->load->library('mongo_db',array('activate'=>'newdb'),'mongo_db_conn');

-1
votes

Refer to this youtube video for easy and few minutes solution to configure mongodb with codeigniter.

https://www.youtube.com/watch?v=SMDznjUz57c