Import data from Mysql to MongoDb with EzSql class

Import data from Mysql to MongoDb with EzSql class

 
 

This simple php class to demonstrate how to import data from a MySql database to a Mongodb with out complications. 




// It uses the ezsql database class from Justin Vincent: 
// http://justinvincent.com/ezsql

include_once "ezsql/shared/ez_sql_core.php";
include_once 
"ezsql/mysql/ez_sql_mysql.php";
          
class 
mongodb_importer
{
    public 
$user;    
    public 
$pass;    
    public 
$databse;    
    public 
$server;    
    public function 
import($table_name,$mongo_db)
    {
        
$db = new ezSQL_mysql($this->user,$this->pass,$this->database,$this->server);
            
        
$m = new Mongo();
        
$mongo_db $m->selectDB($mongo_db);
        
$collection = new MongoCollection($mongo_db$table_name);
        
$res $db->get_results("select * from "$table_name);
        
$i 0;
        foreach(
$res as $r)
        {
            
$i++;
            
$collection->insert($r);    
        }
        return 
$i;
    }
}

?>


And here it is an example about how to use this!

    // create a new object
    
$importer = new mongodb_importer();
    
    
// set your Mysql information
    
$importer->database "my_databse";
    
$importer->pass "my_secret_password";
    
$importer->user "my_user_name";
    
$importer->server "localhost";
    
    
// import the data
    
$count $importer->import("user","new_db");
    
    
// print the result
    
echo "Imported " $count " records.";
?>
 
 


We use our own cookies and third-party cookies to improve our services, show products based on your preferences, analyse the browsing habits of our users, and enable interaction with social networks. Continuing to browse our sites implies full acceptance of their use. You can change your cookie setting or get more information here: Cookies policy .