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.
I completed my post-graduation in 2013 in the engineering field. Engineering is the application of science and math to solve problems. Engineers figure out how things work and find practical uses for scientific discoveries. Scientists and inventors often get the credit for innovations that advance the human condition, but it is engineers who are instrumental in making those innovations available to the world. I love pet animals such as dogs, cats, etc.
Aryan Kumar
27-Jul-2023Sure, 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
csv
andmongodb
modules. 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.js
and 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.