Skip to content

Duplicate Mongoose index on User.email and User.username #4338

Open

Description

@yashgoyal0110

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) and email: { type: String, unique: true } (line 24) create a unique index via unique: 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:

  1. Set up the project locally with a MongoDB connection (copy .env, point MONGO_URL at a running mongo).
  2. Start the server: npm start
  3. Watch the server logs on startup the "Duplicate schema index on {email:1}" warning appears (and the same for username).

Activity

  1. yashgoyal0110 commented on Oct 2, 2026

    @yashgoyal0110
    ContributorAuthor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting Maintainer ApprovalNeeds review from a maintainer before moving forwardBugError or unexpected behaviors

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions