how to integrate mongodb library in codeigniter for database connection with mongodb
Solution 1
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/
Solution 2
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 :)
Admin
Updated on June 12, 2022Comments
-
Admin almost 2 years
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?
-
Lucky Soni over 7 yearsyou are using a deprecated class. I recommend installing the new mongo extension for PHP and using that instead.
-
ρяσѕρєя K about 7 yearsInstead of posting links as answer add some text to explanation how this answer help to OP in fixing current issue.Thanks