How to Seed a MongoDB Database with Fake Data
Rate this tutorial
Have you ever worked on a MongoDB project and needed to seed your database with fake data in order to provide initial values for lookups, demo purposes, proof of concepts, etc.? I'm biased, but I've had to seed a MongoDB database countless times.
First of all, what is database seeding? Database seeding is the initial seeding of a database with data. Seeding a database is a process in which an initial set of data is provided to a database when it is being installed.
This example code uses a single collection of fake IoT data (that I used to model for my
). However, you can change the shape of your template document to fit the needs of your application. I am using
to create the fake data. Please refer to the
if you want to make any changes. You can also adapt this script to seed data into multiple collections or databases, if needed.
Alright, now that we have got the setup out of the way, let's jump into the code!
After running the script above, be sure to check out your database to ensure that your data has been properly seeded. This is what my database looks after running the script above.
Once your fake seed data is in the MongoDB database, you're done! Congratulations!
There are lots of reasons you might want to seed your MongoDB database, and populating a MongoDB database can be easy and fun without requiring any fancy tools or frameworks. We have been able to automate this task by using MongoDB, faker.js, and Node.js. Give it a try and let me know how it works for you! Having issues with seeding your database? We'd love to connect with you. Join the conversation on the