How to Generate MongoDB Seed Data: Complete Guide

Step-by-step guide to generating realistic MongoDB seed data for testing, development, and demos. Includes ObjectId, ISODate, and MongoDB shell script support.

Why Generate MongoDB Seed Data?

MongoDB seed data is essential for:

  • Testing: Test your application with realistic data scenarios and aggregation pipelines
  • Development: Work with data that matches production patterns and document structures
  • Demos: Show your application with populated, realistic documents
  • Performance Testing: Test aggregation pipelines, indexes, and queries with large document collections

MongoDB-Specific Features

Supported MongoDB Types

ID Types

  • • ObjectId (24-character hex)
  • • Automatic _id detection
  • • ObjectId() wrapper in scripts
  • • Referential integrity support

Date Types

  • • ISODate() wrappers
  • • Date and datetime support
  • • Timezone-aware dates
  • • Realistic date ranges

Document Types

  • • Embedded documents (nested objects)
  • • Arrays of values
  • • Mixed types
  • • Flexible schema support

Primitive Types

  • • String, Number, Boolean
  • • Null values
  • • Realistic data patterns
  • • Custom list values

Best Practices for MongoDB Seed Data

1. Use ObjectId for _id Fields

Always use ObjectId type for MongoDB _id fields. MockBlast automatically detects _id columns and generates proper ObjectIds with ObjectId() wrappers. This ensures compatibility with MongoDB's default ID system and allows proper indexing and querying.

2. Wrap Dates with ISODate

Use ISODate() wrappers for all date fields. MockBlast automatically wraps date and datetime fields when MongoDB format is selected, preserving MongoDB's Date type. This allows proper date queries, sorting, and aggregation operations in MongoDB.

3. Maintain Document Relationships

When seeding related collections, ensure parent collections are seeded first. MockBlast handles this automatically by generating parent collections before child collections, maintaining ObjectId references correctly. This ensures referential integrity in your seed data.

4. Test with Various Document Counts

Generate different amounts of data for different testing scenarios. Small datasets (100-1,000 documents) for unit tests, medium datasets (10,000-100,000 documents) for integration tests, and large datasets (1M+ documents) for performance testing and aggregation pipeline optimization.

Example: Complete MongoDB Seed Data Workflow

Step 1: Define Your Schema

CREATE TABLE users (
  _id VARCHAR(24) PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  email VARCHAR(100) UNIQUE NOT NULL,
  created_at TIMESTAMP DEFAULT NOW()
);

CREATE TABLE posts (
  _id VARCHAR(24) PRIMARY KEY,
  user_id VARCHAR(24),
  title VARCHAR(200),
  content TEXT,
  created_at TIMESTAMP DEFAULT NOW()
);

Step 2: Generate MongoDB Seed Data

In MockBlast, select "MongoDB (JS Script)" format and generate your data.

Step 3: Execute in MongoDB Shell

db.users.insertMany([
  { "_id": ObjectId("65a1b2c3d4e5f6789012345a"), "name": "John Doe", "email": "john@example.com", "created_at": ISODate("2024-01-15T10:30:00.000Z") },
  { "_id": ObjectId("65a1b2c3d4e5f6789012345b"), "name": "Jane Smith", "email": "jane@example.com", "created_at": ISODate("2024-01-16T14:20:00.000Z") }
]);

db.posts.insertMany([
  { "_id": ObjectId("65a1b2c3d4e5f6789012345c"), "user_id": ObjectId("65a1b2c3d4e5f6789012345a"), "title": "Getting Started with MongoDB", "content": "Lorem ipsum...", "created_at": ISODate("2024-01-17T08:15:00.000Z") }
]);

Copy and paste this script into MongoDB Shell (mongosh) or MongoDB Compass. The ObjectId() and ISODate() wrappers ensure proper data types.

Importing Seed Data into MongoDB

Once you've generated your MongoDB seed data with MockBlast:

  1. 1.Download the MongoDB shell script file from MockBlast
  2. 2.Connect to your MongoDB instance using MongoDB Shell (mongosh) or MongoDB Compass
  3. 3.Copy and paste the db.collection.insertMany([...]) scripts into your MongoDB client
  4. 4.Execute the scripts. The ObjectId() and ISODate() wrappers ensure proper data types are imported
  5. 5.Verify the data was inserted correctly with a find() query: db.users.find() or db.posts.find()

Related Resources

Frequently Asked Questions

Does MockBlast support MongoDB ObjectId generation?
Yes! MockBlast has native ObjectId support. Fields named _id are automatically detected and generated as ObjectId type with ObjectId() wrappers. You can also manually set any field to ObjectId type for references between documents.
How do I generate ISODate wrappers for MongoDB dates?
MockBlast automatically wraps all date and datetime fields with ISODate() when you select MongoDB format. This preserves MongoDB's Date type, allowing proper date queries and sorting. Unlike plain JSON imports, dates remain as Date objects, not strings.
Can I generate seed data for multiple MongoDB collections?
Absolutely! MockBlast supports multi-table schemas that convert to multiple MongoDB collections. Each collection gets its own db.collection.insertMany() script. Parent collections are generated first, then child collections with proper ObjectId references.
What format does MockBlast generate for MongoDB?
MockBlast generates native MongoDB shell scripts in the format db.collection.insertMany([...]). These scripts include ObjectId() and ISODate() wrappers and can be copy-pasted directly into MongoDB Shell (mongosh) or MongoDB Compass. No conversion needed.
How large can my MongoDB seed data be?
MockBlast can generate millions of documents efficiently using server-side streaming. Perfect for seeding large MongoDB databases or testing aggregation pipelines and performance. Memory usage stays constant regardless of document count.
Can I use the generated scripts with MongoDB Atlas?
Yes! The generated MongoDB shell scripts work with MongoDB Atlas, MongoDB Community Edition, and any MongoDB-compatible database. Simply connect to your Atlas cluster and execute the scripts in MongoDB Shell or Compass.

Ready to Generate Mock Data?

Stop writing scripts manually. MockBlast generates production-ready seed data for Postgres, MySQL, MongoDB, and JSON in seconds.