This script first imports the MongoClient module from the mongodb package. It then defines the MongoDB connection URL, which is
mongodb://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 the
people 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:
node seed.js
This will seed your MongoDB database with the sample data.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is a Node.js script to seed your MongoDB database with sample data:
JavaScript
This script first imports the
MongoClientmodule from themongodbpackage. 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 thepeoplecollection. 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.jsand then run it from the command line:This will seed your MongoDB database with the sample data.