Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syncIndex failed creating index for the field with name 'key' #15056

Open
2 tasks done
1nstinct opened this issue Nov 22, 2024 · 0 comments
Open
2 tasks done

syncIndex failed creating index for the field with name 'key' #15056

1nstinct opened this issue Nov 22, 2024 · 0 comments

Comments

@1nstinct
Copy link

Prerequisites

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

Mongoose version

8.7.0

Node.js version

18.18.0

MongoDB server version

6.0.19

Typescript version (if applicable)

No response

Description

Schema

const ObjectKeySchema = new mongoose.Schema({
  key: {
    type: String,
    required: true,
    unique: true,
  },
  type: {
    type: String,
    required: false,
  },
}, {
  timestamps: false,
  versionKey: false,
});

ObjectKeySchema.index({ key: 1 });

The index key_1 already exists in the DB, but the function call ObjectKey.syncIndexes() throws an error:

MongoServerError: An existing index has the same name as the requested index. When index names are not specified, they are auto generated and can cause conflicts. Please refer to our documentation. Requested index: { v: 2, key: { key: 1 }, name: "key_1", background: true }, existing index: { v: 2, unique: true, key: { key: 1 }, name: "key_1", background: true }
at Connection.sendCommand (/data/node_modules/mongodb/lib/cmap/connection.js:289:27)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Connection.command (/data/node_modules/mongodb/lib/cmap/connection.js:312:26)
at async Server.command (/data/node_modules/mongodb/lib/sdam/server.js:167:29)
at async CreateIndexesOperation.executeCommand (/data/node_modules/mongodb/lib/operations/command.js:73:16)
at async CreateIndexesOperation.execute (/data/node_modules/mongodb/lib/operations/indexes.js:122:9)
at async tryOperation (/data/node_modules/mongodb/lib/operations/execute_operation.js:192:20)
at async executeOperation (/data/node_modules/mongodb/lib/operations/execute_operation.js:69:16)
at async Collection.createIndex (/data/node_modules/mongodb/lib/collection.js:326:25) {
  errorResponse: {
    ok: 0,
    errmsg: 'An existing index has the same name as the requested index. When index names are not specified, they are auto generated and can cause conflicts. Please refer to our documentation. Requested index: { v: 2, key: { key: 1 }, name: "key_1", background: true }, existing index: { v: 2, unique: true, key: { key: 1 }, name: "key_1", background: true }',
    code: 86,
    codeName: 'IndexKeySpecsConflict',
    '$clusterTime': {
      clusterTime: new Timestamp({ t: 1732303578, i: 14 }),
      signature: [Object]
    },
    operationTime: new Timestamp({ t: 1732303578, i: 14 })
  },
  ok: 0,
  code: 86,
  codeName: 'IndexKeySpecsConflict',
  '$clusterTime': {
    clusterTime: new Timestamp({ t: 1732303578, i: 14 }),
    signature: {
      hash: Binary.createFromBase64('OIA61Np4Zxmq4QBf1Ih5rquR7m0=', 0),
      keyId: new Long('7398342335731335169')
    }
  },
  operationTime: new Timestamp({ t: 1732303578, i: 14 }),
  [Symbol(errorLabels)]: Set(0) {}
}

Steps to Reproduce

  1. Create schema
  2. Run syncIndex()

Expected Behavior

Should ignore creating existing index.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant