简体   繁体   中英

How to import CSV data into mongoDB using PHP

I have a CSV file with data like the following:

cn,mail,telephonenumber,uid
admin,,,
"Isaac Newton",newton@ldap.forumsys.com,,newton
"Albert Einstein",einstein@ldap.forumsys.com,314-159-2653,einstein

I would like to make it so that in mongodb, my data would look like the following:

{ "_id" : ObjectId("52c10f355b9e5cf00200014d"), "Name" : "admin", "Email" : "Null", "Phone Number" : "Null", "User ID" : "Null" }
{ "_id" : ObjectId("52c10f355b9e5cf00200014d"), "Name" : "Isaac Newton", "Email" : "newton@ldap.forumsys.com", "Phone Number" : "Null", "User ID" : "newton" }
{ "_id" : ObjectId("52c10f355b9e5cf00200014d"), "Name" : "Albert Einstein", "Email" : "einstein@ldap.forumsys.com", "Phone Number" : "314-159-2653", "User ID" : "einstein" }

For all the null fields, they can just not exist either, but wanted to make it clear that the fields are missing.

The only information I've found is in this forum, but uses the deprecated mongo PHP driver documentation.

Importing csv into mongodb using PHP code

However, here's some code that I've written to insert a single document.

<?php
require 'vendor/autoload.php';

$mongo_location = "mongodb://localhost:27017";
$mongo = new MongoDB\Driver\Manager($mongo_location);
echo "Connected to ".$mongo_location;
  $bulk = new MongoDB\Driver\BulkWrite;

  $doc = array(
      'id'      => new MongoDB\BSON\ObjectID,     #Generate MongoID
      'name'    => 'harry',
      'age'     => 14,
      'roll_no' => 1
  );

  $bulk->insert($doc);
  $mongo->executeBulkWrite('schooldb.student', $bulk);    # 'schooldb' is database and 'student' is collection.
?>

As you can see, the way you connect to the mongodb is different now.

Here's a solution:

  1. Install a CSV to JSON package with Composer like this:

composer require ozdemirburak/json-csv

  1. Place a students.csv file at your project's root. Contents:
name,email,phoneNumber,userID
admin,,,
Isaac Newton,newton@ldap.forumsys.com,,newton
Albert Einstein,einstein@ldap.forumsys.com,314-159-2653,einstein
  1. Place a students.php file at your project's root. Contents:
<?php
use OzdemirBurak\JsonCsv\File\Csv;
require 'vendor/autoload.php';

$csv_filename = 'students.csv';
$csv = new Csv($csv_filename);
echo 'Loaded CSV file: ' . $csv_filename . '<br>';

// Convert CSV to JSON then JSON to Array.
$array = json_decode($csv->convert(), true);

$mongo_location = 'mongodb://localhost:27017';
$mongo = new MongoDB\Driver\Manager($mongo_location);
echo 'Connected to ' . $mongo_location . '<br>';

$mongo_bulk_write = new MongoDB\Driver\BulkWrite();
foreach ($array as $doc) {
  // MongoDB ObjectID will be automatically generated.
  $mongo_bulk_write->insert($doc);
}
// 'schooldb' is database and 'student' is collection.
$mongo->executeBulkWrite('schooldb.student', $mongo_bulk_write);
echo 'Populated MongoDB database';
?>
  1. Open students.php in your Web browser. Check MongoDB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM