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 · 1 comment · May be fixed by #15093
Open
2 tasks done

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

1nstinct opened this issue Nov 22, 2024 · 1 comment · May be fixed by #15093
Labels
developer-experience This issue improves error messages, debugging, or reporting
Milestone

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.

@vkarpov15 vkarpov15 added this to the 8.8.4 milestone Nov 26, 2024
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Nov 26, 2024
@vkarpov15 vkarpov15 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Dec 4, 2024
@vkarpov15 vkarpov15 removed this from the 8.8.4 milestone Dec 4, 2024
@vkarpov15
Copy link
Collaborator

So the issue is that you're defining two separate indexes: one by specifying unique: true on the key schema path, and one with ObjectKeySchema.index({ key: 1 });. Not a Mongoose bug, the fix is to remove the ObjectKeySchema.index({ key: 1 }); call, which is unnecessary.

There are a couple of things Mongoose can do in this case, since the error message reported could be better.

  1. Throw an error if there are multiple indexes with the exact same keys in a schema
  2. Reconcile these index calls and make it so that ObjectKeySchema.index({ key: 1 }) is a no-op since there's already an index

I think (1) is the way to go, because (2) leads to unnecessary code that could easily be deleted, and would likely be more complex to implement. I'll put in a fix.

@vkarpov15 vkarpov15 added this to the 8.9.1 milestone Dec 13, 2024
@vkarpov15 vkarpov15 added developer-experience This issue improves error messages, debugging, or reporting and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary labels Dec 13, 2024
vkarpov15 added a commit that referenced this issue Dec 13, 2024
… in schema path and subsequent `.index()` call

Fix #15056
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
developer-experience This issue improves error messages, debugging, or reporting
Projects
None yet
2 participants