How to Seed a MongoDB Database with Fake Data

Joe KarlssonBlog

How to Seed a MongoDB Database with Fake Data

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.
Screenshot showing the seeded data in a MongoDB Atlas cluster.

Once your fake seed data is in the MongoDB database, you’re done! Congratulations!

Want to check out more of my technical posts?

Follow Joe Karlsson on Social

Want to Learn More About Joe Karlsson?