Database

How to Seed MongoDB with Realistic Test Data

The Problem

MongoDB is schemaless, which sounds like freedom until you need to populate it with realistic test data. Without a schema, most developers default to { name: "Test User 1" } and call it done. The result is a database that tells you nothing about how your app will look or perform in production.

If your collections reference each other (users and orders, for example), maintaining those references with Faker.js means generating one collection, collecting IDs, then passing them into the next. It is tedious and error-prone.

The Solution: MockHero API

MockHero takes a JSON schema with 156+ field types and ref fields for cross-collection references. One API call returns consistent, linked documents you can insert directly into MongoDB.

Quick Setup

curl -X POST https://api.mockhero.dev/api/v1/generate \
  -H "x-api-key: mh_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
  "tables": [
    {
      "name": "users",
      "count": 10,
      "fields": [
        { "name": "_id", "type": "object_id" },
        { "name": "username", "type": "username" },
        { "name": "email", "type": "email" },
        { "name": "bio", "type": "sentence" }
      ]
    },
    {
      "name": "posts",
      "count": 30,
      "fields": [
        { "name": "_id", "type": "object_id" },
        { "name": "authorId", "type": "ref", "params": { "ref": "users._id" } },
        { "name": "title", "type": "sentence" },
        { "name": "body", "type": "paragraphs" },
        { "name": "tags", "type": "array", "params": { "itemType": "word", "min": 1, "max": 4 } }
      ]
    }
  ],
  "format": "json"
}'

Step-by-Step Guide

1. Install dependencies

npm install mongodb

2. Get your MockHero API key

Sign up at mockhero.dev/sign-up.

3. Write the seed script

Create seed.mjs:

import { MongoClient } from "mongodb";

const client = new MongoClient(process.env.MONGO_URI);
await client.connect();
const db = client.db("myapp");

const res = await fetch("https://api.mockhero.dev/api/v1/generate", {
  method: "POST",
  headers: {
    "x-api-key": process.env.MOCKHERO_API_KEY,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    tables: [
      {
        name: "users",
        count: 10,
        fields: [
          { name: "_id", type: "object_id" },
          { name: "username", type: "username" },
          { name: "email", type: "email" },
          { name: "bio", type: "sentence" },
        ],
      },
      {
        name: "posts",
        count: 30,
        fields: [
          { name: "_id", type: "object_id" },
          { name: "authorId", type: "ref", params: { ref: "users._id" } },
          { name: "title", type: "sentence" },
          { name: "body", type: "paragraphs" },
          { name: "tags", type: "array", params: { itemType: "word", min: 1, max: 4 } },
        ],
      },
    ],
    format: "json",
  }),
});

const { data } = await res.json();

await db.collection("users").insertMany(data.users);
await db.collection("posts").insertMany(data.posts);

console.log("Seeded", data.users.length, "users and", data.posts.length, "posts");
await client.close();

4. Run it

node seed.mjs

5. Verify in MongoDB Compass

Open Compass and browse the users and posts collections. Every post's authorId matches a real user _id.

Complete Example

The script above is production-ready. MockHero's ref field guarantees every authorId maps to an existing user, and insertMany handles the batch write efficiently.

Why MockHero vs Faker / Manual Seeds

  • Cross-collection referencesref fields link documents without manual ID juggling.
  • MongoDB-native types — use object_id for proper Mongo IDs out of the box.
  • Bulk-insert ready — arrays map directly to insertMany.

Get Started

Free tier, no credit card. Sign up at mockhero.dev and populate your MongoDB in minutes.

M

MockHero Team

Guides and tutorials for generating realistic test data with the MockHero API.

Start generating test data for free

1,000 rows/month on the free tier. No credit card required.

Get Your API Key

Related Articles