mysql - Inserting an array into MongoDB -
i have table in mysql called new_ndnc
contains 5 fields. each field contains 10 million rows.
i have read each field array, , want insert whole array field in mongodb.
my code below.
#!/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 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 new_ndnc'); $abcuid->execute; @uid; while (my @row = $abcuid->fetchrow_array()) { push(@uid, @row); } $hh = $dbh->prepare('select phonenumbers new_ndnc'); $hh->execute; @route1; while (my @row = $hh->fetchrow_array()) { push(@route1, @row); } $r4 = $dbh->prepare('select preferences new_ndnc'); $r4->execute; @route4; while (my @row = $r4->fetchrow_array()) { push(@route4, @row); } $exr4 = $dbh->prepare('select opstype new_ndnc'); $exr4->execute; @exroute4; while (my @row = $exr4->fetchrow_array()) { push(@exroute4, @row); } $r5 = $dbh->prepare('select phonetype 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]" } );
on re-reading code approach wrong. doing pulling out of table 1 column @ time, pushing each row value array , trying write mongodb. stands trying write single document in mongodb has each field containing each row value table. , not want.
what likely, want is:
select results table columns going put in each mongo field. , in one query.
as fetch on rows of table, insert each document mongodb collection.
another misconception have objectid's (oid). each mongodb document has default field _id
, if not specify value explicitly on insert value populated automatically. if have reasonable natural primary key not duplicate can put field. in hashref notation, { '_id' => 'key_value' }.
for easier transposition of table rows hashref structure need inserting row mongodb, @ fetchrow_hashref in dbi documentation.
it's kind of hard advise beyond code kind of funky , it's not clear trying do. these aren't small arrays don't want try , push values in 1 document.
Comments
Post a Comment