Hot File

How to create use mongo DB in codeigniter

View: 556    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: Codeigniter   Fields: Other

18 point/3 review File has been tested

While working in scalable projects, we can go with ‘ NO sql ‘ database and ‘ Mongodb’ is one of the ‘ NoSQL’ databases. In this post,we are going to see how to use mongodb in codeigniter

Introduction

While working in scalable projects, we can go with ‘ NO sql ‘ database and ‘ Mongodb’ is one of the ‘ NoSQL’ databases. In this post,we are going to see how to use mongodb in codeigniter

Codeigniter is one of PHP framework and it is also a popular framework because of its simple MVC architecture.We can integrate any type of database with codeigniter and there are lot of database library class for their integration. For Mongodb integration also, there are few library classes but there are little bit difficult to understand and use them in our application. Here I have created a simple library class to connect codeigniter with mongodb. Once we connect codeigniter with this library class, we can use mongodb as like we use mongodb in raw php. In you want to know how to use mongodb with php, then please see my previous post about it [ Mongodb with PHP ]
Initially, we need to setup our mongodb database config file in codeigniter . For this purpose, we need to create a file inside ‘config’ directory of your application [ Folder path : YOURPROJECT/application/config] and give a file name as ‘ mongodb.php ‘ . In this file, we will the following information

1) Database name
2) Database username
3) Database password

Configuration

config/mongodb.php

//mongodb host
$config['default']['mongo_hostbase'] = 'localhost';
//mongodb name
$config['default']['mongo_database'] = 'linkexchange';
//mongodb username - by default, it is empty
$config['default']['mongo_username'] = '';
//mongodb password - by default, it is empty
$config['default']['mongo_password'] = '';

Once we setup mongodb config file, then we need to create mongodb library class and then connect the mongodb with codeigniter. Let us create a file called ‘ Mongo_db.php ‘ inside library folder of your application [ Folder path : YOURPROJECT/application/libraries ] . In this file, we will connect the codeigniter with mongodb .I hope, the below code is self understandable.

Library class

libraries/Mongo_db.php

_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);
}
 
}

Now we have connected our codeigniter application with mongodb. In our controllers, we can call the mongodb and fetch records from its collection

For example, let us create simple controller and fetch some records from mongodb

Let us create a controller called ‘Sample.php’ in controller directory of our application [ folder path : YOURPROJECT/application/controllers ] .
In that file, we will have the following code

Controller

controllers/sample.php

class Sample extends CI_Controller{
 
public function __construct(){
 
parent::__construct();
}
 
//index of our controller
function index() {
 
//load the mongodb library
$this->load->library('mongo_db');
 
//connect to mongodb collection named as 'category' using our mongodb library
$collection = $this->mongo_db->db->selectCollection('category');
 
//fetch the record from that collection
$result=$collection->find();
foreach($result as $document) {
//display the records
var_dump($document);
}
}
 
}

I have created this mongodb library for simple usecase and it is easy for me to use mongodb in codeigniter as like mongodb with raw php. If you want, you can enhance the above code to suit your needs. please tell your suggestion by comments

How to create use mongo DB in codeigniter

How to create use mongo DB in codeigniter Posted on 15-02-2016  While working in scalable projects, we can go with ‘ NO sql ‘ database and ‘ Mongodb’ is one of the ‘ NoSQL’ databases. In this post,we are going to see how to use mongodb in codeigniter 6/10 556

Comment:

To comment you must be logged in members.

Files with category

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd