我正在尝试将mysql表导入到mongodb集合中。我想将mysql列作为mongodb子文档: MYSQL =>
| col1 | col2 | col3 | col4 |
------------------------------
| abc | def | ghi | jkl |
------------------------------
| 1234 | 5678 | 9876 | 4567 |
------------------------------
| abc | def | ghi | jkl |
------------------------------
etc.....
在mongodb中希望它们看起来像: MongoDB =>
> doc1: {
> col1["abc", "1234", "abc", .....],
> col2["def", "5678", "def", .....],
> col3["ghi", "9876", "ghi", .....],
> col4["jkl", "4567", "jkl", .....],
> etc.
我使用php,我的代码如下:
$sql=mysql_query("select * from mytable") or die (mysql_error());
$count=mysql_num_rows($sql);
if (count > 0) {
while($data=mysql_fetch_row($sql)){
$mosql[]=$data;
}
$collection -> insert($mosql)
}
作为我得到的输出,它给了我行而不是列:
doc1:{ col1:[0:“abc”,1:“def”,2:“ghi”,3:“jkl”.....], col2:[0:“1234”,1:“5678”,2:“9876”,3:“4567”......], col3:[0:“abc”,1:“def”,2:“ghi”,3:“jkl”.....], 等
有人知道我做错了什么吗? 谢谢你的帮助!
答案 0 :(得分:1)
您可能想尝试循环遍历从MySQL返回的行数组并将文档一个接一个地插入MongoDB集合,而不是一次插入整个数组。
答案 1 :(得分:1)
最后我想出了如何做到这一点,可能这种方式太长但我得到了我想要的东西:
//var77
foreach( $mytables as $table => $struct ) {
$sql77 = mysql_query("SELECT WINDSHEAR FROM XL_10331_EXPLOIT_251012 where flightid like '191622'") or die( mysql_error() );
$count77 = mysql_num_rows( $sql77 );
// If it has content insert all content
if( $count77 > 0 ) {
while($info77 = mysql_fetch_row($sql77)) {
$mosql77[]=$info77[0];
}
//var78
foreach( $mytables as $table => $struct ) {
$sql78 = mysql_query("SELECT WING_AI_SYS_ON FROM XL_10331_EXPLOIT_251012 where flightid like '191622'") or die( mysql_error() );
$count78 = mysql_num_rows( $sql78 );
// If it has content insert all content
if( $count78 > 0 ) {
while($info78 = mysql_fetch_row($sql78)) {
$mosql78[]=$info78[0];
}
$collection->insert(array('flightid'=>191622, 'AI_PB_ON_1'=>$mosql77, 'AI_PB_ON_2'=>$mosql78, etc....));