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

匆匆过客 提交于 2019-12-05 11:40:47

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/

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 :)

aishwarya

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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!