Repository navigation
Duplicate Mongoose index on User.email and User.username #4338
Description
p5.js version
N/A this is a server-side (database model) issue, unrelated to the p5.js library version.
What is your operating system?
Mac OS
Web browser and version
N/A backend issue, happens at server startup before any browser is involved.
Actual Behavior
On server startup, Mongoose logs a duplicate-index warning:
Duplicate schema index on {"email":1} found. This is often due to declaring an index using both "index: true" and "schema.index()". Please remove the duplicate index definition.
The same happens for username.
In server/models/user.ts the fields are indexed twice:
username: { type: String, required: true, unique: true }(line 15) andemail: { type: String, unique: true }(line 24) create a unique index viaunique: true- lines 333–334 declare a second index on the same fields with a collation:
userSchema.index({ username: 1 }, { collation: { locale: 'en', strength: 2 } });
userSchema.index({ email: 1 }, { collation: { locale: 'en', strength: 2 } });
On top of the warning, the two indexes aren't equivalent. The unique: true index is case-sensitive and is the only one enforcing uniqueness; the collation index (meant to make lookups case-insensitive) is not unique. So although the model comments say "Email addresses should be case-insensitive unique" and findByEmailAndUsername queries with a collation, the database only enforces case-sensitive uniqueness case-insensitive uniqueness depends entirely on the app-level check at signup (which has a check-then-insert race).
Expected Behavior
No duplicate-index warning at startup, and a single case-insensitive unique index per field so the database itself enforces the case-insensitive uniqueness the code already assumes.
Suggested fix- move uniqueness onto the collation index:
// schema fields
username: { type: String, required: true }, // drop unique: true
email: { type: String }, // drop unique: true
// bottom of file
userSchema.index({ username: 1 }, { unique: true, collation: { locale: 'en', strength: 2 } });
userSchema.index({ email: 1 }, { unique: true, collation: { locale: 'en', strength: 2 } });
(Existing deployments would need the old case-sensitive indexes dropped so the new ones get built, and any pre-existing case-duplicate rows cleaned up first.)
Steps to reproduce
Steps:
- Set up the project locally with a MongoDB connection (copy .env, point MONGO_URL at a running mongo).
- Start the server: npm start
- Watch the server logs on startup the "Duplicate schema index on {email:1}" warning appears (and the same for username).
Activity
yashgoyal0110 commentedon Oct 2, 2026
@ksen0 @clairep94