我试图将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“……等等。
有人知道我做错了什么吗?谢谢你的帮助!
发布于 2013-02-28 16:53:20
最后我想出了如何做到这一点,也许这条路太长了,但我得到了我想要的:
//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....)); 发布于 2013-02-27 14:42:33
您可能需要尝试循环遍历从MySQL返回的行数组,然后一个接一个地将文档插入到MongoDB集合中,而不是一次性插入整个数组。
https://stackoverflow.com/questions/15114713
复制相似问题