Simple Utility For Copying Data From MongoDB To MySql


SUBMITTED BY: Guest

DATE: June 12, 2013, 1:12 a.m.

FORMAT: PHP

SIZE: 3.9 kB

HITS: 819

  1. <?php
  2. $sql = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
  3. mysql_select_db($a_db, $sql);
  4. $m = new MongoClient($dbName);
  5. $db = $m->$a_mongo_db;
  6. $cols = $db->getCollectionNames();
  7. // WATCH OUT!!!! Drops existing tables from MySql,
  8. foreach ($cols as $k => $v)
  9. {
  10. $drop = "DROP TABLE IF EXISTS $v";
  11. $reply = mysql_query($drop, $sql);
  12. }
  13. foreach ($cols as $k => $v)
  14. {
  15. $c = $db->$v;
  16. $r = $c->find();
  17. $fields = array();
  18. $fieldTypes = array();
  19. foreach ($r as $rec)
  20. {
  21. foreach ($rec as $fname => $field)
  22. {
  23. $ftype = tgetType($field);
  24. if ($fname == "synonyms")
  25. {
  26. }
  27. else if ($ftype == "array")
  28. {
  29. foreach ($field as $subName => $sub_val)
  30. if ($sub_val != null)
  31. {
  32. if ($subName == 'date')
  33. {
  34. $f2type = 'DATE';
  35. }
  36. else
  37. $f2type = tgetType($sub_val);
  38. if ($f2type == 'DATE')
  39. $fieldTypes[$fname . '_' . $subName] = 'DATETIME';
  40. else if ($f2type == "OBJECT")
  41. $fieldTypes[$fname . '_' . $subName] = 'VARCHAR(255)';
  42. else if ($f2type != "array")
  43. $fieldTypes[$fname . '_' . $subName] = $f2type;
  44. }
  45. }
  46. else if ($ftype == "OBJECT")
  47. {
  48. $fieldTypes[$fname] = 'VARCHAR(255)';
  49. $fields[$fname] = true;
  50. }
  51. else if ($field != null)
  52. {
  53. $fieldTypes[$fname] = $ftype;
  54. $fields[$fname] = true;
  55. }
  56. }
  57. }
  58. // create MySql tables
  59. $createCmd = "create table $v (";
  60. foreach ($fieldTypes as $name => $type)
  61. {
  62. $createCmd.="$name $type";
  63. if ($name == '_id')
  64. $createCmd.=" PRIMARY KEY,";
  65. else
  66. $createCmd.=',';
  67. }
  68. $createCmd = trim($createCmd, ',');
  69. $createCmd.=")";
  70. $out = mysql_query($createCmd);
  71. if ($out == false)
  72. echo $createCmd . "\n";
  73. // inserting data
  74. $r = $c->find();
  75. foreach ($r as $rec)
  76. {
  77. $cmd = "insert into $v set ";
  78. foreach ($rec as $fname => $field)
  79. {
  80. $ftype = tgetType($field);
  81. if ($fname == "synonyms")
  82. {
  83. }
  84. else if ($ftype == "array")
  85. {
  86. foreach ($field as $subName => $sub_val)
  87. if ($sub_val != null)
  88. {
  89. if ($subName == 'date')
  90. $f2type = 'DATE';
  91. else
  92. $f2type = tgetType($sub_val);
  93. if ($f2type == "DATE")
  94. {
  95. $cmd.=$fname . "_$subName='$sub_val' ";
  96. $cmd.=',';
  97. }
  98. else if ($f2type == "OBJECT")
  99. {
  100. $val = $sub_val->__toString();
  101. $cmd.=$fname . "_$subName='$val' ";
  102. $cmd.=',';
  103. }
  104. else if ($f2type != "array")
  105. {
  106. if ($f2type == "BIT" || $f2type == "INT")
  107. $cmd.=$fname . "_$subName=$sub_val ";
  108. else
  109. {
  110. $sub_val = remLetters($sub_val);
  111. $cmd.=$fname . "_$subName='$sub_val' ";
  112. }
  113. $cmd.=',';
  114. }
  115. }
  116. }
  117. else if ($ftype == "OBJECT")
  118. {
  119. $val = $field->__toString();
  120. $cmd.="$fname='$val' ";
  121. $cmd.=',';
  122. }
  123. else if ($field != null)
  124. {
  125. if ($ftype == "BIT" || $ftype == "INT")
  126. $cmd.="$fname=$field ";
  127. else
  128. {
  129. $field = remLetters($field);
  130. $cmd.="$fname='$field' ";
  131. }
  132. $cmd.=',';
  133. }
  134. }
  135. $cmd = trim($cmd, ',');
  136. $out = mysql_query($cmd);
  137. if ($out == false)
  138. echo 'SQL error:' . $cmd . "\n";
  139. }
  140. }
  141. // Mapping from Mongo types to MySql types, feel free to change
  142. function tgetType($field)
  143. {
  144. if (is_string($field))
  145. return "TEXT";
  146. else if (is_object($field))
  147. return "OBJECT";
  148. else if (is_bool($field))
  149. return "BIT";
  150. else if (is_int($field))
  151. return "INT";
  152. else if (is_object($field))
  153. return "VARCHAR(255)";
  154. else if (is_array($field))
  155. {
  156. return "array";
  157. }
  158. }
  159. // this is done to avoid SQL errors, but it changes the strings, removing quotes and double quotes
  160. function remLetters($s)
  161. {
  162. $remove[] = "'";
  163. $remove[] = '"';
  164. $out = str_replace($remove, " ", $s);
  165. return $out;
  166. }

comments powered by Disqus