How to integrate mongodb library into codeigniter to connect to database with mongodb

I am creating an application with a codeigniter and mongodb database. Since codeigniter does not contain the mongodbs driver, I used the built-in library to connect to mongodb. I used the link to create the mongodb configuration file and the Mongo_db library. The configuration file is similar to the \ config \ mongodb application:

<?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 the Mongo_db library:

<?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 launch the application, it gives me an error, for example, an error has been detected. The application / config / mongodb.php file does not contain a valid configuration array.

Can anybody say How to solve this error and establish connection with mongodb?

+4
source share
4 answers

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

/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);

:)

+1

mongoDB codeIgniter , . mongoDB .

  • paccage PECL.

PHP - http://php.net/manual/en/install.pecl.php

  1. CodeIgniter.

, mongoDB

  1. mongoDB.

mongoDB . https://docs.mongodb.com/manual/installation .

  1. .

, , application/config, , /. mongoDB ,

$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. .

, mongoDB , mongoDB. .

$ this-> load-> library ('mongo_db', array ('Activate' => 'NEWDB'), 'mongo_db_conn');

For more help check out tekina.info

0
source

Check out this YouTube video for an easy and little minute solution to configure mongodb with codeigniter.

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

-1
source

All Articles