EventGet 50% off your ticket to MongoDB.local NYC on May 2. Use code Web50!Learn more >>
MongoDB Developer
MongoDB
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
MongoDBchevron-right

How to Seed a MongoDB Database with Fake Data

Joe Karlsson2 min read • Published Jan 28, 2022 • Updated Sep 23, 2022
MongoDB
Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
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.
In this post, you will learn how to get a working seed script setup for MongoDB databases using Node.js and faker.js.

The Code

This example code uses a single collection of fake IoT data (that I used to model for my IoT Kitty Litter Box project). However, you can change the shape of your template document to fit the needs of your application. I am using faker.js to create the fake data. Please refer to the documentation if you want to make any changes. You can also adapt this script to seed data into multiple collections or databases, if needed.
I am saving my data into a MongoDB Atlas database. It's the easiest way to get a MongoDB database up and running. You'll need to get your MongoDB connection URI before you can run this script. For information on how to connect your application to MongoDB, check out the docs.
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.
Screenshot showing the seeded data in a MongoDB Atlas cluster.
Once your fake seed data is in the MongoDB database, you're done! Congratulations!

Wrapping Up

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 MongoDB Community Forums.

Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Article

Real-Time Location Updates with MongoDB Stitch, Change Streams, and Mapbox


Feb 03, 2023 | 5 min read
News & Announcements

Halting Development on MongoDB Swift Driver


Apr 04, 2024 | 1 min read
Tutorial

Utilizing PySpark to Connect MongoDB Atlas with Azure Databricks


Apr 02, 2024 | 6 min read
Article

Java Driver: Migrating From 4.11 to 5.0


Mar 01, 2024 | 3 min read
Table of Contents
  • The Code