Convention: How to handle Mongoose duplicate schema index warnings?
Documentation for Sessions
📏 Convention: How to handle Mongoose duplicate schema index warnings?
Date: 2026-02-10
Time: 17:02:19 UTC
Session: gspace1-102.9
Agent: backend
Confidence: 95%
Status: ✅ Documented
📋 Decision Context
Question:
How to handle Mongoose duplicate schema index warnings?
Task: Fix Mongoose duplicate index warnings - Session 102.3
🎯 Decision Made
Chosen:
Never combine index: true with unique: true in @Prop (unique already creates an index). Never add schema.index({field: 1}) when @Prop already has index: true for the same single field. Never add schema.index({field: 1}, {expireAfterSeconds}) when @Prop has expires option. For compound indexes, remove index: true from @Prop if the field is the first key in a compound schema.index() call.
Options Considered:
- Ignore warnings
- Fix duplicates systematically using the rule: one index definition per field
💭 Reasoning
Mongoose warns about duplicate schema indexes when the same {field: 1} index is defined twice via different mechanisms. Common patterns that cause duplicates: unique+index in @Prop, @Prop({sparse:true}) + schema.index(), @Prop({expires:N}) + schema.index({field:1},{expireAfterSeconds:N}). These create redundant indexes that waste storage and slow writes.
📁 Files Involved
apis/backend-api/src/app/ai-agents/schemas/agent-embedding.schema.tsapis/backend-ai-api/src/app/schemas/agent-registry.schema.tsapis/backend-docs-api/src/app/docs-sync/schemas/doc-file.schema.ts
🔗 Related
- Decision ID:
dev_1770742939604_24srf5j - Session: 102
- Workspace: gspace1
Auto-generated by MCP Smart Consolidation