Skip to content

index setting error occurred at 8.9.1("Schema already has an index") #15109

@0Ams

Description

@0Ams

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Issue

Error message

MongooseError: Schema already has an index on {"uid":1,"gid":1}
    at Schema.index (xxx/node_modules/mongoose/lib/schema.js:2151:13)
    at Object.<anonymous> (xx/schema/member.js:20:8)

Schema

/* eslint-disable */
const mongoose = require('mongoose');

const _schema = {
    uid: {type: mongoose.Schema.Types.ObjectId, required: true},
    gid: {type: mongoose.Schema.Types.ObjectId, required: true},
};
const schema = new mongoose.Schema(
    _schema,
    {'versionKey': false, 'usePushEach': true, 'minimize': false, 'shardKey': {'uid': 1}}
);
schema.index({'�gid': 1, 'uid': 1}, {background: false});
schema.index({'uid': 1, 'gid': 1}, {background: false});

mongoDB

test> db.test.createIndex({uid: 1, gid:1})
uid_1_gid_1
test> db.test.createIndex({uid: 1, gid:1})
uid_1_gid_1
test> db.test.getIndexes()
[
  { v: 2, key: { gid: 1, uid: 1 }, name: 'gid_1_uid_1' },
  { v: 2, key: { uid: 1, gid: 1 }, name: 'uid_1_gid_1' }
]

In my opinion, the two indexes seem to work differently in MongoDB.
why is this causing an error in mongoose? It works correctly in mongoose@8.8.x, but it behaves abnormally in mongoose@8.9.1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    developer-experienceThis issue improves error messages, debugging, or reporting

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions