【发布时间】:2014-03-02 23:54:17
【问题描述】:
我在 MySQL 中有一个名为 new_ndnc 的表,其中包含五个字段。每个字段包含 1000 万行。
我已将每个字段读入一个数组,现在我想将整个数组插入到 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]"
}
);
【问题讨论】:
-
请总是
use strict和use warnings在您编写的每个 Perl 程序的开头,尤其是在您寻求帮助时. -
@user2916639 我正在使用#perl -w 运行代码
-
use warnings在命令行上比-w更可取,因为它是词法范围的,并且可以打开或关闭各个警告类别。而use strict甚至比启用警告更重要。 -
到目前为止的建议很好。但是您的 MongoDB 部分在哪里?你试过什么。顺便说一句,@Borodin 告诉您要使用 strict ,因为您正在到处声明全局变量。会回来咬你。
-
@NeilLunn;
$users变量是 MongoDB 数据库amrit上名为hell2的集合。最后一条语句是尝试写入它。