mongodb-schema-design

Guide MongoDB schema design to prevent performance and data integrity issues.

4|2|Updated May 18, 2022
One-click install
npx skills add https://github.com/pjmagee/starwars-data --skill mongodb-schema-design-pjmagee
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: mongodb-schema-design
Source: https://github.com/pjmagee/starwars-data/tree/main/.agents/skills/mongodb-schema-design
Command: npx skills add https://github.com/pjmagee/starwars-data --skill mongodb-schema-design-pjmagee

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

MongoDB schema design patterns and anti-patterns to prevent performance, cost, and data-integrity issues caused by poor document modeling. It helps engineers choose between embedding and referencing, avoid unbounded arrays and unnecessary indexes, and apply validation and migration strategies that keep production stable.

Core Features & Use Cases

  • Schema fundamentals: guidance on the document model, embed vs reference decisions, and document size constraints including the 16MB BSON limit.
  • Design patterns & anti-patterns: actionable patterns (approximation, bucket, time-series, archive, attribute, outlier, polymorphic, extended-reference, computed, document-versioning, schema-versioning) and anti-patterns (excessive $lookup, unnecessary collections, unnecessary indexes).
  • Verification & migration: step-by-step checks, JSON Schema validation guidance, safe online migration strategies, index audit and safe removal process, and optional MCP-based read-only verification with explicit write approval controls.
  • Use Case: review a legacy SQL-derived product catalog and produce a denormalized MongoDB design that minimizes joins, limits index bloat, and preserves query performance while supporting safe rollouts.

Quick Start

Ask the skill to review a specific collection's schema and recommend embed-versus-reference changes, index removals, and validation rules to improve performance and reduce storage cost.

Frequently Asked Questions about mongodb-schema-design

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I choose between embedding and referencing when designing MongoDB schemas?

MongoDB schema design balances embedding versus referencing by evaluating data access patterns, document size limits, and query performance. Embed related data accessed together to avoid excessive lookups, and reference data when arrays grow unbounded or exceed the 16MB BSON limit.

What is the best way to migrate a relational SQL schema to MongoDB without losing data integrity?

Migrating relational schemas to MongoDB requires denormalizing SQL joins into embedded documents or references, applying JSON Schema validation, and following safe online migration workflows. This approach minimizes join operations while preserving data integrity during production rollouts.

How do I model time-series data in MongoDB to prevent unbounded array growth?

Time-series data modeling in MongoDB uses bucket and approximation patterns to group temporal records into bounded documents. This prevents unbounded array growth, keeps documents under the 16MB BSON limit, and optimizes indexing for time-based queries.

When should I remove redundant indexes from my MongoDB collections?

Redundant index removal should occur during an index audit when duplicate or unused indexes cause storage bloat and slow write performance. Perform safe removal workflows after verifying hot-path queries to ensure no critical read operations depend on the targeted indexes.

Does MongoDB schema design support JSON Schema validation for data integrity?

MongoDB schema design supports JSON Schema validation to enforce data integrity, structure, and required fields at the document level. Applying validation rules during collection creation or migration prevents malformed data from entering the database.