Ask Your Question
0

What is the process for correctly closing a MongoDB connection and inserting data from a CSV file using node file streams?

asked 2023-06-16 08:24:29 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-06-16 08:34:02 +0000

pufferfish gravatar image

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
})
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-16 08:24:29 +0000

Seen: 10 times

Last updated: Jun 16 '23