With IBM moving to MongoDB for IBM Connections in my mind, i decided to migrate one of my smallest websites from PHP – MySQL to PHP – MongoDB.
Wow, what a difference in the positive way, and how easy it is to migrate your data from MySQL into MongoDB.
You don’t need to create a database, create a table. After defining your dataset, everything is created automatically.
I created a website for my imporant dates so i cannot forget them. On this website, i created a countdowntimer for every single important date.
Migratie data from MySQL to MongoDB
For this part of the migration, i decided to write my own script.
database: datesangioni
Table into Collection: months with 2 columns: monthnumber and monthdescription
Table into Collection: dates with 7 columns: dateid, description, dateday, datemonth, dateyear, time and divname
Attention: Read my article MongoDB Change of Class
PHP MySQL database configuration
$host="localhost"; $dbuser="<databaseuser>"; $dbpassword="<databaseuserpassword"; $databasename="dates"; $db = mysqli_connect($host , $dbuser, $dbpassword, $databasename);
PHP MongoDB configuration
$m = new MongoClient('mongodb://<databaseuser>:<databaseuserpassword>@<ipaddress>/datesangioni');
$db = $m->datesangioni;
PHP migration script for table months
//open collection
$collection = $db->months;
$query_read = "select * from months order by monthnumber";
$result = mysqli_query($db, $query_read);
while( $row = mysqli_fetch_array($result, MYSQLI_ASSOC) ){
$document = array(
"monthnumber" => $row['monthnumber'],
"monthdescription" => $row['monthdescription'];
);
$collection->insert($document);
}
PHP migration script for table dates
// open collection
$collection = $db->dates;
$query_read = "select * from dates";
$result = mysqli_query($db, $query_read);
while( $row = mysqli_fetch_array($result, MYSQLI_ASSOC) ){
$document = array(
"dateid"= $row['DateID'],
"description"= $row['description'],
"dateday"= $row['dateday'],
"datemonth"= $row['datemonth'],
"dateyear"= $row['dateyear'],
"time"= $row['time'],
"divname"= $row['divname'],
);
$collection->insert($document);
}
$list = $db->listCollections();
Changed PHP code from MySQL to MongoDB
/////// open database
$m = new MongoClient('mongodb://<databaseuser>:<databaseuserpassword>@<ipaddress>/datesangioni');
$db = $m->datesangioni;
/////////// read months into array
$collection = $db->months;
$cursor = $collection->find();
foreach ($cursor as $document) {
$maand[$document['monthnumber']]=$document['description'];
}
/////////// read dates into array
$collection = $db->dates;
$cursor = $collection->find();
foreach ($cursor as $document) {
$names[]=$document['description'];
$dates[]=$document['dateday'] . ' ' . $month[$document['datemonth']] . ' ' . substr('0000' . $document['dateyear'],-4);
$times[]=$document['time'];
$dvnaam[]=$document['divname'];
}
And the result is a faster and MongoDB running backend.
Views: 370

