问题
Im using MongoCollection::batchInsert() to insert csv data into mongodb,
<?php
$mongo = new Mongo("mongodb://192.168.1.7:27017");
$collection = $mongo->test->cartoons;
$row = 1;
$handle = fopen(dirname(Yii::app()->request->scriptFile).'/images/importcsv/upload.csv', "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
$users = array();
for ($c=0; $c < $num; $c++) {
echo 'data--'.$data[$c] . "<br />\n";
}
$users[] =$data;
var_dump($users);
$collection->batchInsert($users);
}
?>
the above code inserts data in Mongo Collections as two separate arrays one line for header(CSV header) and another one is for data.
Now i want to merge the CSV header with data as like below to import in mongo,
{ "_id" : ObjectId("52c10f355b9e5cf00200014d"), "Name" : "Dhanam R.", "DOB" : "12-Aug-80", "Age" : "80" }
回答1:
Really this question could use some Googling but this time only, what you do is take the first row of the CSV:
$columns = fgetcsv($fh, 0, ',');
$nrColumns = count($columns);
for ($i=0; $i < $nrColumns; $i++)
{
if(array_key_exists($columns[$i], $columnsArray)){
$columnsArray[$columns[$i].$col_inc] = $i;
$col_inc++;
}else{
$columnsArray[$columns[$i]] = $i;
}
}
And use that to compare to the index of what you have in $c
:
$user = array();
for ($c=0; $c < $num; $c++) {
$user[$columnsArray[$c]] = $data[$c]
echo 'data--'.$data[$c] . "<br />\n";
}
and then you use $user
to add to the array:
$users[] =$user;
回答2:
create CSV file in Mongo Db using php code given below:-
<?php
set_time_limit(0);
ob_start();
// connect to mongodb
$mongo = "172.00.22.00:27017";
$conn = new MongoClient("mongodb://$mongo");
if (!$conn) {
die("Unable to connect with mongodb");
}
$db = $conn->nexg_tmp;
//header('Content-Type: application/csv');
//header('Content-Disposition: attachment; filename=example.csv');
//header('Pragma: no-cache');
//$regex = new MongoRegex("/^LIV/");
//$where = array('code' => $regex);
$col1 = $db->ASSETS;
$col2 = $db->BITFRAME;
//$records = $col1->find($where)->limit(5);
//$records = $col1->find()->limit(1500);
$records = $col1->find();
//$records = $col1->find(array('code' => 'LIV002'))->limit(5);
$csvarr = array();
$fp = fopen('example.csv', 'w');
$headings[] = "Code";
$headings[] = "CH/VOD CODE";
$headings[] = "CH/VOD NAME";
$headings[] = "VOD/LIVE";
$headings[] = "URL1";
$headings[] = "URL2";
fputcsv($fp, $headings);
foreach ($records as $val) {
$csvarr = array();
$csvarr['code'] = $val['code'];
$csvarr['charge_code'] = $val['charge_code'];
if (empty($val['name'])) {
$val['name'] = $val['channel_name'];
}
/* if(empty($val['type'])){
$val['type'] ="VOD";
} */
$csvarr['name'] = @$val['name'];
$csvarr['type'] = @$val['type'];
$rec_BitFrameCol = $col2->find(array('code' => $val['code'], 'type' => $val['type']));
foreach ($rec_BitFrameCol as $k => $BitFrameval) {
$csvarr['url' . $k] = $BitFrameval['url'];
}
fputcsv($fp, $csvarr);
}
echo "Completed Successfully..";
?>
来源:https://stackoverflow.com/questions/20834396/importing-csv-into-mongodb-using-php-code