Home » Coding

MySQL to MongoDB

10 May 2012 No Comment

After trying this script that i wrote yesterday [PHP] MySQL result to JSON – For mongoDB found out it’s not very useful if the MySQL result is big (like in my case) and 570MB of JSON encoded data is not very practical to upload and import.

So instead i wrote another little script to export a table and push it to the new mongo collection (table).


//mysql_query("SET NAMES 'utf8';");

$m = new Mongo("mongodb://HOST:PORT");
// If mongoDB is on the same box then use $m = new Mongo();
$db = $m->local; // "local" is the name of the database
$collection = $db->mytable; //
"mytable" is the name of the mongoDB collection/database

$m = mysql_query('SELECT * FROM `mytable` LIMIT 0,10000;');
//mytable is mysql's table name

$i = 0;
while($rows[] = mysql_fetch_assoc($m)){
	$insert = $rows[$i];
	//Get the array and insert it into MongoDB
print 'done!';

Your opinion matters!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.