我在MySQL中有一個名爲new_ndnc
的表,它包含五個字段。每個字段包含1000萬行。將數組插入到MongoDB中
我已經將每個字段讀入一個數組,現在我想將整個數組插入到MongoDB中的字段中。
我的代碼如下。
#!/usr/bin/perl
use MongoDB;
use MongoDB::OID;
use DBI;
$dbs = 'amrit';
$user = 'root';
$pass = 'walkover';
$dbh = DBI->connect("DBI:mysql:database=$dbs", $user, $pass)
or die "Cannot connect to MySQL server\n";
$conn = MongoDB::Connection->new(
host => 'localhost',
port => 27017,
db_name => 'amrit'
);
$db = $conn->get_database('amrit');
$users = $db->get_collection('hell2');
$abcuid = $dbh->prepare('select Service_Area_Code from new_ndnc');
$abcuid->execute;
@uid;
while (my @row = $abcuid->fetchrow_array()) {
push(@uid, @row);
}
$hh = $dbh->prepare('select phonenumbers from new_ndnc');
$hh->execute;
@route1;
while (my @row = $hh->fetchrow_array()) {
push(@route1, @row);
}
$r4 = $dbh->prepare('select Preferences from new_ndnc');
$r4->execute;
@route4;
while (my @row = $r4->fetchrow_array()) {
push(@route4, @row);
}
$exr4 = $dbh->prepare('select Opstype from new_ndnc');
$exr4->execute;
@exroute4;
while (my @row = $exr4->fetchrow_array()) {
push(@exroute4, @row);
}
$r5 = $dbh->prepare('select PhoneType from new_ndnc');
$r5->execute;
@route5;
while (my @row = $r5->fetchrow_array()) {
push(@route5, @row);
}
$users->insert({
'UserID' => "[@uid]",
'route1' => "[@route1]",
'route4' => "[@route4]",
'route4_extra_bal' => "[@exroute4]",
'route5' => "[@route5]"
}
);
請在你編寫的每一個* Perl程序開始時,總是*使用strict和'use warnings',特別是如果你需要幫助的話。 – Borodin
@ user2916639我使用#perl -w – user2916639
運行代碼在命令行中''warnings''優於'-w',因爲它在詞彙範圍內,並且可以打開或關閉各個警告類別。 「嚴格使用」比啓用警告更重要。 – Borodin