Mongo DB

                                                                                                        << BACK

Database Connection MongoDB to CodeIgniter
Model :
----------------
In Model Write Connection Function :
function connection($tableName){
// Connect to Mongo
//$this->connection = new MongoClient('49.248.35.122:27017'); //for linux
$this->connection = new Mongo('192.168.1.26:27017'); // for window
// Select a database
$this->db = $this->connection->rajesh; //rajesh is database
// Select a collection
$this->posts = $this->db->$tableName;
}
---------------------------------------------------------------------------------------------------------
Save Data Or Update Data :
function save($connection,$member='') {
$this->connection($connection);
if ($member != '') {
if (!isset($member['id'])) { // new record
$this->posts->insert($member);
return $member['_id'];
} else { // edit existing record
$memberid = $member['id'];
$this->posts->update(array('_id' => new MongoId($memberid)), $member, array("multiple" => false));
return $memberid;
}
}
}
---------------------------------------------------------------------------------------------------------
Update Data :
Controllers Part :
  $status_id          = $this->input->post('status_id');        
  $status_name        = $this->input->post('status_name');
$status_number        = $this->input->post('status_number');
$member = array(
"status_name"=>"$status_name",
"status_number"=>"$status_number"
);
$connection = 'ht_admin_status_details';

$this->home_model->update($connection,$status_id,$member);
Model Part :
function update($connection,$mongo_id,$dataArray) {
        $this->connection($connection);
        $members = $this->posts->update( array('_id' =>  new MongoId($mongo_id)), array('$set' => $dataArray ),
            array("upsert" => true)
        );
        return $members;
    }
---------------------------------------------------------------------------------------------------------
get All record :
function getAll($connection) {
$this->connection($connection);
$members = $this->posts->find();
return $members;
}
---------------------------------------------------------------------------------------------------------
get All record By order Ascending or Descending :
$orderby = array('date_time' => 1); //if record want to Ascending Order
$orderby = array('date_time' => -1); //if record want to Descending Order
function getAllOrderData($connection,$orderby) {
$this->connection($connection);
$members = $this->posts->find();
if ($orderby != ''){$members = $members->sort($orderby);}
return $members;
}
---------------------------------------------------------------------------------------------------------
get record By using Like Query Where One Filed and One Value :
function getWhereLikeData($connection,$fieldName,$id){       
$this->connection($connection);  
$regex = new MongoRegex('/'.$id.'/i');     
$members = $this->posts->find(array("$fieldName" => $regex));      
return $members;
}
---------------------------------------------------------------------------------------------------------
get record By using Like and Order Query Where One Filed and One Value :
function getWhereDataLikeorderby ($connection,$fieldName,$id,$orderby){
$this->connection($connection); 
$regex = new MongoRegex('/'.$id.'/i');               
$members = $this->posts->find(array("$fieldName" => $regex));
if ($orderby != ''){$members = $members->sort($orderby);}
return $members;
}
---------------------------------------------------------------------------------------------------------
get record By using Where Clause One Filed and One Value :
function getWhereData($connection,$fieldName,$id=''){
$this->connection($connection);        
$members = $this->posts->find(array("$fieldName" => $id));
return $members;
}
---------------------------------------------------------------------------------------------------------
get record By using Where Clause Two Filed and Two Value :
function getAndWhereData ($connection,$fieldName1,$id1,$fieldName2,$id2){
$this->connection($connection);        
$members = $this->posts->find(array("$fieldName1" => $id1,"$fieldName2" => $id2));      
return $members;
}
---------------------------------------------------------------------------------------------------------
get Search(Like) In Two Filed With One Value :
function getWhereDataOrLike($connection,$fieldName1,$fieldName2,$id=''){
$this->connection($connection); 
$regex = new MongoRegex('/'.$id.'/i');
$members =  $this->posts->find(array('$or' => array(
array("$fieldName1" => $regex),
array("$fieldName2" => $regex)
)));        
return $members;
}
---------------------------------------------------------------------------------------------------------
get record By order Ascending or Descending using Where Clause One Filed and One Value :
function getWhereOrderData($connection,$fieldName,$id,$orderby){
$this->connection($connection);        
$members = $this->posts->find(array("$fieldName" => $id));
if ($orderby != ''){$members = $members->sort($orderby);}
return $members;
}
---------------------------------------------------------------------------------------------------------
get record By order Ascending or Descending using Where Clause Two Filed and Two Value :
function getAndWhereOrderData ($connection,$fieldName1,$id1,$fieldName2,$id2,$orderby){
$this->connection($connection);        
$members = $this->posts->find(array("$fieldName1" => $id1,"$fieldName2" => $id2));
if ($orderby != ''){$members = $members->sort($orderby);}
return $members;
}
---------------------------------------------------------------------------------------------------------
get limited record By using Where Clause One Filed and One Value :
function getWhereLimitData($connection,$fieldName,$id,$limit){
$this->connection($connection);        
$members = $this->posts->find(array("$fieldName" => $id));
if ($limit != ''){ $members->limit($limit);}        
return $members;
}
---------------------------------------------------------------------------------------------------------
get limited record By order Ascending or Descending Where Clause One Filed and One Value :
function getWhereLimitOrderData($connection,$fieldName,$limit,$id,$orderby){
$this->connection($connection);        
$members = $this->posts->find(array("$fieldName" => $id));
if ($limit != ''){ $members->limit($limit);}
if ($orderby != ''){$members = $members->sort($orderby);}   
return $members;
}
---------------------------------------------------------------------------------------------------------
get record by using collection ID(Unique ID) :
function getByID($connection,$id='') {
$this->connection($connection);
$member = $this->posts->findOne(array('_id' => new MongoId($id)));        
if ($member) {
return $member;
}
return false;
}
---------------------------------------------------------------------------------------------------------
get remove record by using collection ID(Unique ID) :
function deleteById($connection,$id) {
$this->connection($connection);
$this->posts->remove(array('_id' => new MongoId($id)), array("justOne" => true));
}
---------------------------------------------------------------------------------------------------------
get record by using Or Clause :
function getOrData($connection,$col1,$val1,$col2,$val2){
$this->connection($connection); 
$members = $this->posts->find(array('$or' => array(array('$col1' => "$val1"),array('$col2' => "$val2"))));    
return $members;      
}
---------------------------------------------------------------------------------------------------------
get record by using In Clause :
function getInData($connection,$col,$val){
$this->connection($connection); 
$members = $this->posts->find(array('$col'=> array('$in'=> array("$val"))));    
return $members;      
}
---------------------------------------------------------------------------------------------------------
get record by using Between Clause :
function getBetweenData($connection,$col,$val1,$val2){
$this->connection($connection); 
$members = $this->posts->find(array($col => array('$gt'=> "$val1",'$lt'=> "$val2")));
return $members;

---------------------------------------------------------------------------------------------------------
get record by using Between Where Clause :
function getBetweenWhereData($connection,$col,$val1,$val2,$where_col,$where_val){
$this->connection($connection); 
$members = $this->posts->find(array('$where_col' => "$where_val",$col => array('$gt'=> "$val1",'$lt'=> "$val2")));
return $members;
}
---------------------------------------------------------------------------------------------------------
get record by using two or condition and one and condition Where Clause :
//$sql = "SELECT * FROM $tbl where (( $fld1 = '$val1' or $fld1 = '$val2') and ( $fld2 = '$val1' or $fld2 = '$val2')) $orderby";
function getAND_TWOOR_WhereOrderData( $connection, $fieldName1, $fieldName2, $val1, $val2, $orderby){
$this->connection($connection); 
$members =  $this->posts->find(
array(
'$and' => array (
array( '$or' => array( array ("$fieldName1"=>$val1), array ("$fieldName1" => $val2))
 ),
array( '$or' => array( array ("$fieldName2"=>$val1), array ("$fieldName2" => $val2)))
 )
)
);   
if ($orderby != ''){$members = $members->sort($orderby);}
return $members;
}
==========================================================
Controller :
------------------------
In controller function get data by using model function
$play = $this->home_model->getAndWhereData('sharing_details' ,'shared_type','0','status','0');
$data = array(
'play_list' => array()
);
while($play->hasNext()){
$playData = $play->getNext();
$data['play_list'][] = array(
'play_id' => $playData["_id"]->__toString(),
'user_id'       => $playData['user_id']
);
}
==========================================================
IMP Command Ubuntu:
Extract tgz file:

---------------------------------------------------------------------------------------------------------
Give all permission to folder:
---------------------------------------------------------------------------------------------------------
Install JDK:
---------------------------------------------------------------------------------------------------------
Install Notepad++


---------------------------------------------------------------------------------------------------------
Installing Phonegap Software:
---------------------------------------------------------------------------------------------------------
Install mongo.so (.dll):
---------------------------------------------------------------------------------------------------------
Extract File:
---------------------------------------------------------------------------------------------------------
Uninstall Program:
---------------------------------------------------------------------------------------------------------
Unzip Rar File:
---------------------------------------------------------------------------------------------------------
XAMPP SERVER START:
---------------------------------------------------------------------------------------------------------
Install mongo dll in xampp (window) :
Install mongo dll in xampp using window system
run localhost to browser http://localhost/xampp/

first check xampp php version e.g.: [PHP: 5.5.19]
and then click on phpinfo()
check PHP Extension Build i.e. API20121212,TS,VC11 see last word VC11

base on php version e.g. 5.5 to download zip file to following link

extract file match file name (php_mongo-1.5.5-5.5-vc11.dll)

more information to see following image

copy dll file and past D:\xampp\php\ext location and rename file php_mongo-1.5.5-5.5-vc11.dll to php_mongo.dll
Then go to php.ini file, after 
extension=php_mysql.dll write 
extension=php_mongo.dll
and restart xampp server

and run http://localhost/xampp/ click on phpinfo() check mongo installed
---------------------------------------------------------------------------------------------------------







3 comments: