Sunday, 5 June 2016

How to migrate MongoDB Collection's with PHP?



  1. MongoDB is an NOSQL DB which is very faster in fetching the records from huge data, from a cluster of DB' S with sharding.
  2. The delete operation is slower when compared to SQL DB' s.


Here I gave an example for Migrating MongoDB Records which all are inserted in last 24 hours.

Step1: Installing PHP MongoDB database driver
           aptitude install php5-mongo

Step2: Global Constants
         
    Create MongoDB Connection Strings
              public static $connString = "mongodb://123.45.67.89:27017";
              public static $connStringL = "mongodb://127.0.0.1:27017";

    DB, Collection Constants
             public static $db="db";
             public static $dbl="db";
             public static $coll_A="A";


Step3: Function to establish a connection with MongoDB
   
    /*to connect with source mongodb*/
    public function connection(){
        return new MongoClient(self::$connString);
    }

    /*to connect with destination mongodb*/
    public function connectionL(){
        return new MongoClient(self::$connStringL);
    }

Step4: Linux date to timestamp which we need to pass in the MongoDB Query
    public function convertDate2TimeStamp($date){
        $timestamp=strtotime(date($date));
        return $timestamp;
    }

Step5: MongoDB Query Construction and get the result in a Cursor

            $Query=(array('tstamp'=> array('$gt' => $start,'$lte' => $end)));
            $cursor=$coll->find($Query);


Step6: Iterating through Cursor

            while(($doc=$cursor->getNext())!=null){
                 try{
                $res=$collL->insert($doc);
/*
                if($res['ok']!=1)
                    error_log("Insert Failed..",3,"/var/log/mongo-cron/mongo.log");

                foreach($doc as $k => $v){
                 echo PHP_EOL.$v;
                  }*/
}catch(Exception $e){
//skipping the errors
}
            }



Complete Program
-----------------------
class mongocron {

    public static $connString = "mongodb://123.45.67.89:27017";
    public static $connStringL = "mongodb://127.0.0.1:27017";

    public static $db="db";
    public static $dbl="db";
    public static $coll_A="A";


    public function connection(){
        return new MongoClient(self::$connString);
    }

    public function connectionL(){
        return new MongoClient(self::$connStringL);
    }

    public function convertDate2TimeStamp($date){
        $timestamp=strtotime(date($date));
        return $timestamp;
    }

    public function migrate_coll_A(){
        try{
            $timestampStart=$this->convertDate2TimeStamp(date('Y-m-d',strtotime("-1 days")));
            $timestampEnd=$timestampStart+(60*60*23.999999);

            $start=new MongoDate($timestampStart);
            $end=new MongoDate($timestampEnd);

    /* Migrating Collection A */

            $conn=$this->connection();
            $db=$conn->selectDB(self::$db);
            $coll=$db->selectCollection(self::$coll_A);
            $Query=(array('tstamp'=> array('$gt' => $start,'$lte' => $end)));
            $cursor=$coll->find($Query);

            $connL=$this->connectionL();
            $dbL=$connL->selectDB(self::$dbl);

            //$collL=$dbL->selectCollection(self::$collectionL.'-'.date('m-Y',strtotime("-1 days")));
            $collL=$dbL->selectCollection(self::$coll_A);

            //  echo PHP_EOL.date('Y-m-d H:i:s',$start);
            //  echo PHP_EOL.date('Y-m-d H:i:s',$end);

            while(($doc=$cursor->getNext())!=null){
                 try{
                $res=$collL->insert($doc);
/*
                if($res['ok']!=1)
                    error_log("Insert Failed..",3,"/var/log/mongo-cron/mongo.log");

                foreach($doc as $k => $v){
                 echo PHP_EOL.$v;
                  }*/
}catch(Exception $e){
//skipping the errors
}
            }

error_log(date('Y/m/d H:i:s').": Records A migrated\n",3,"/var/log/mongo-cron/mongo.log");

        }catch (Exception $e){
            error_log(date('Y/m/d H:i:s').":A:: unknown Exception:".$e->getMessage(). "\n",3,"/var/log/mongo-cron/mongo.log");
        }
    }


$obj = new mongocron();
$obj->migrate_coll_A();
?>


No comments:

Post a Comment