
TL;DR
A quick Node.js script using faker.js to generate thousands of fake documents and insert them into a MongoDB Atlas database. Copy it, tweak the document shape, and you have seed data in seconds.
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.
How to Seed MongoDB
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!
/* mySeedScript.js */
// require the necessary libraries
const faker = require('faker');
const MongoClient = require('mongodb').MongoClient;
function randomIntFromInterval(min, max) {
// min and max included
return Math.floor(Math.random() * (max - min + 1) + min);
}
async function seedDB() {
// Connection URL
const uri = 'YOUR MONGODB ATLAS URI';
const client = new MongoClient(uri, {
useNewUrlParser: true,
// useUnifiedTopology: true,
});
try {
await client.connect();
console.log('Connected correctly to server');
const collection = client.db('iot').collection('kitty-litter-time-series');
// The drop() command destroys all data from a collection.
// Make sure you run it against proper database and collection.
collection.drop();
// make a bunch of time series data
let timeSeriesData = [];
for (let i = 0; i < 5000; i++) {
const firstName = faker.name.firstName();
const lastName = faker.name.lastName();
let newDay = {
timestamp_day: faker.date.past(),
cat: faker.random.word(),
owner: {
email: faker.internet.email(firstName, lastName),
firstName,
lastName,
},
events: [],
};
for (let j = 0; j < randomIntFromInterval(1, 6); j++) {
let newEvent = {
timestamp_event: faker.date.past(),
weight: randomIntFromInterval(14, 16),
};
newDay.events.push(newEvent);
}
timeSeriesData.push(newDay);
}
collection.insertMany(timeSeriesData);
console.log('Database seeded! :)');
client.close();
} catch (err) {
console.log(err.stack);
}
}
seedDB();
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 like 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! If you want a nice way to browse and verify your seeded data, I’d recommend the MongoDB Visual Studio Code plugin for exploring your collections right from your editor.
More Technical Posts

Joe Karlsson
Developer Marketing Engineer at CData, leading developer growth for the managed MCP platform that connects AI agents to live enterprise data. Writing about databases, self-hosting, and the things I build. Runs a 60+ container Proxmox homelab with AI-powered automations.
cat newsletter.md
Works on My Machine is my newsletter: thoughts and reactions on tech writing, AI, homelab builds and open source, plus whatever I'm working on. It goes out when I have something to say.
Free, no spam, unsubscribe anytime. Read past issues.
Related Posts

Mar 30, 2024
Modern data management with real-time Change Data Capture
Data engineers are grappling with surging data volumes while stakeholders increasingly demand real-time decision-making and user-facing features powered by data. This raises a fundamental question:...

Oct 16, 2020
Save The World And Money With MongoDB Atlas Data Lake
Thanks for coming to my talk, Save The World And Money With MongoDB Atlas Data Lake. About Data centers are expensive. It turns out that this is not great for the environment. By 2040, storing...
Feb 24, 2026
Visibility-First Cloud Governance: Why You Should Start with Data
Most governance models fail because they enforce rules about infrastructure they cannot see. The Visibility-First model starts with a queryable foundation.