Write a Node.js script to seed your MongoDB database with sample data.
Write a Node.js script to seed your MongoDB database with sample data.
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 seed your MongoDB database with sample data:
JavaScript
This script first imports the
MongoClient
module from themongodb
package. It then defines the MongoDB connection URL, which ismongodb://localhost:27017/mydb
.Next, the script defines an array of sample data. The sample data consists of three people, with their names, ages, and addresses.
The
seedDatabase()
function then connects to the MongoDB database and gets thepeople
collection. The function then inserts the sample data into the collection.Finally, the
seedDatabase()
function logs a message to the console indicating that the database has been seeded successfully.To run this script, you can save it as a file called
seed.js
and then run it from the command line:This will seed your MongoDB database with the sample data.