Write a Node.js script to read data from a CSV file and store it in a MongoDB database.
Write a Node.js script to read data from a CSV file and store it in a MongoDB database. Steilla Mitchel 659 26 Jul 2023 Write a Node.js script to read data from a CSV file and store it in a MongoDB database.
Sure, here is a Node.js script to read data from a CSV file and store it in a MongoDB database:
JavaScript
This script first imports the
csvandmongodbmodules. Then, it creates a new MongoDB client and connects to the database.Next, the script opens the CSV file and parses it into an array of objects. Each object in the array represents a row in the CSV file.
Finally, the script iterates through the array of objects and inserts each object into the MongoDB collection.
To run this script, you can save it as a file called
read_csv.jsand then run it from the command line:This will read the data from the CSV file and store it in the MongoDB database. You can then query the database to view the data.