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:

  1. Ignore warnings
  2. 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


🔗 Related


Auto-generated by MCP Smart Consolidation