Import data from Mysql to MongoDb with EzSql class

Import data from Mysql to MongoDb with EzSql class
by Janeth Kent Date: 11-04-2013

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.";
?>
 
by Janeth Kent Date: 11-04-2013 hits : 4188  
 
Janeth Kent

Janeth Kent

Licenciada en Bellas Artes y programadora por pasión. Cuando tengo un rato retoco fotos, edito vídeos y diseño cosas. El resto del tiempo escribo en MA-NO WEB DESIGN AND DEVELOPMENT.

 
 
 
Clicky