Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The process for correctly closing a MongoDB connection and inserting data from a CSV file using node file streams can be:

  1. Establish a connection to MongoDB using the MongoDB driver for Node.js.
const mongodb = require('mongodb');
const MongoClient = mongodb.MongoClient;
const url = 'mongodb://localhost:27017/my-database';

MongoClient.connect(url, (err, client) => {
  // handle errors and continue
});
  1. Create a database and collection instance.
const db = client.db('my-database');
const collection = db.collection('my-collection');
  1. Use the createReadStream() function from the node fs module to read the CSV file and create a stream.
const fs = require('fs');
const csv = require('csv-parser');

fs.createReadStream('path/to/file.csv')
  .pipe(csv())
  .on('data', (data) => {
    // handle data and insert into database
  })
  .on('end', () => {
    // handle end of file and close connection
  });
  1. For each row of data, insert it into the collection using the insertOne() method.
collection.insertOne(data, (err, result)=> {
  if (err) {
    // handle errors
  } else {
    console.log(`Inserted document with _id: ${result.insertedId}`);
  }
});
  1. Once the file has been completely read and all data has been inserted into the collection, close the MongoDB connection using the close() method.
client.close();
  1. Handle any errors that may occur during the reading and insertion of data.
.on('error', (err) => {
  // handle errors
})