mongodb-schema-audit

Audits Mongoose schemas for missing indexes, broken relationships, and unsafe migrations.

3|1|Updated Apr 8, 2026
One-click install
npx skills add https://github.com/ever-just/agentskills --skill mongodb-schema-audit-ever-just
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: mongodb-schema-audit
Source: https://github.com/ever-just/agentskills/tree/main/skills/mongodb-schema-audit
Command: npx skills add https://github.com/ever-just/agentskills --skill mongodb-schema-audit-ever-just

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? MongoDB and Mongoose fail silently: queries on non-existent fields return empty results, missing indexes cause full collection scans, and relationship references are never enforced. This Skill provides a systematic procedure to audit schemas, map query patterns to indexes, and run safe data migrations before these issues reach production. ## Core Features & Use Cases - Index Coverage Auditing: Find schemas with no indexes, map every find/aggregate query pattern to a supporting compound or single-field index, and add them before model export. - Relationship Validation: Verify that Mongoose ref directions are correct so queries like Inbox.findOne({ agentId }) do not silently return null on non-existent fields. - Idempotent Data Migrations: Run rename/transform migrations that return { migrated, total }, handle empty results gracefully, use .lean() for reads, and report errors to Sentry. - Schema Verification Tests: Write bun test suites that assert required fields, ref directions, and index definitions exist in schema files before every deploy. - Use Case: An admin dashboard joins Messages by organizationId and sorts by createdAt; this Skill detects the missing compound index { organizationId: 1, createdAt: -1 } and adds it before the query scans the full collection. ## Quick Start Ask the agent to audit all Mongoose schemas in db/mongo/schemas for missing indexes and incorrect relationship directions, then add the required indexes and verification tests.

Frequently Asked Questions about mongodb-schema-audit

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

FAQPage Schema
How do I find missing indexes in Mongoose schemas?▼

Grep schema files for .index( declarations and flag any schema file with none. Then map every Model.find, countDocuments, and aggregate call to a supporting index, adding compound indexes like { organizationId: 1, createdAt: -1 } for filtered sorted queries.

Why does my MongoDB query return null without an error?▼

MongoDB does not validate query fields against the schema, so querying a non-existent field like Inbox.findOne({ agentId }) silently returns null. Always verify the field actually exists on the target schema and that the ref direction is correct before querying.

How do I write a safe MongoDB data migration?▼

Make migrations idempotent so they can run multiple times, return a { migrated, total } payload for auditability, and return early when no documents match. Use .lean() for read-only fetches and wrap the handler in try/catch with Sentry error capture.

Does Mongoose enforce relationship references between collections?▼

No, Mongoose refs define relationships but do not enforce them at the database level. You must verify the reference direction manually, for example Agent owns inboxId so you query Inbox.findById(agent.inboxId), not the reverse.

Why does adding a unique index fail on existing MongoDB data?▼

Adding { unique: true } to a field that already contains duplicate values fails at startup because MongoDB cannot build the index. Clean or deduplicate the existing data first, or handle the index build error explicitly.