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()
{
if ( ! class_exists('Mongo'))
{
$this->_show_error('The MongoDB PECL extension has not been installed or enabled', 500);
}
if (function_exists('get_instance'))
{
$this->_ci = get_instance();
}
else
{
$this->_ci = NULL;
}
$this->_ci->load->config('mongodb');
$config='default';
$config_data = $this->_ci->config->item($config);
try{
$this->mb = new Mongo('mongodb://'.$config_data['mongo_hostbase']);
$this->db=$this->mb->selectDB($config_data['mongo_database']);
}
catch (MongoConnectionException $exception)
{
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?
source
share