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

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

Closed
2 tasks done
0Ams opened this issue Dec 17, 2024 · 9 comments · Fixed by #15112 or #15135
Closed
2 tasks done

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

0Ams opened this issue Dec 17, 2024 · 9 comments · Fixed by #15112 or #15135
Labels
developer-experience This issue improves error messages, debugging, or reporting
Milestone

Comments

@0Ams
Copy link

0Ams commented Dec 17, 2024

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 [email protected], but it behaves abnormally in [email protected].

@maxuai
Copy link

maxuai commented Dec 17, 2024

We had the same error for a slightly different scenario:

we had declared the unique keyword twice, once in the schema and second time in a separate index creation:

const schema = new Schema({myId: {type String, required: true, unique: true}})
schema.index({myId: 1}, {unique: true})

removing the unique from the schema declaration fixed it.

@ronenteva
Copy link

I have the same issue with this use case:

schema.index({email: 1, region: 1}, {unique: true});
schema.index({email: 1, region: 1}, {name: 'email_1_region_1-active', partialFilterExpression: {active: true}});

@ilkkaparssinen
Copy link

Error seems to be in new code for 8.9.1 (lib/schema.js):
for (const existingIndex of this.indexes()) {
if (util.isDeepStrictEqual(existingIndex[0], fields)) {
throw new MongooseError(Schema already has an index on ${JSON.stringify(fields)});
}
}

isDeepStrictEqual-function returns true for index definitionswhich have same fields in different order. Normally in javascript objects the order of properties should not matter. For mongoose/mongodb the order if items is meaningful:
(We got the error for these two indexes):
schema.index({ at: -1, url: 1 });
schema.index({ url: 1, at: -1 });

@vkarpov15 vkarpov15 added this to the 8.9.2 milestone Dec 17, 2024
@vkarpov15 vkarpov15 added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Dec 17, 2024
vkarpov15 added a commit that referenced this issue Dec 17, 2024
fix(schema): avoid throwing duplicate index error if index spec keys have different order or index has a custom name
@ecofi
Copy link

ecofi commented Dec 20, 2024

When can we expect a new release with a fix? This broke all our DB connections and difficult to port back to older mongoose release. Can this have a priority?

@winterrdog
Copy link

We had the same error for a slightly different scenario:

we had declared the unique keyword twice, once in the schema and second time in a separate index creation:

const schema = new Schema({myId: {type String, required: true, unique: true}})
schema.index({myId: 1}, {unique: true})

removing the unique from the schema declaration fixed it.

ran into the same issue & this seems to have fixed it 🤝

@orgads
Copy link
Contributor

orgads commented Dec 25, 2024

I have another regression with nestjs. See nestjs/mongoose#2346.

@ecofi
Copy link

ecofi commented Dec 25, 2024

We had the same error for a slightly different scenario:
we had declared the unique keyword twice, once in the schema and second time in a separate index creation:

const schema = new Schema({myId: {type String, required: true, unique: true}})
schema.index({myId: 1}, {unique: true})

removing the unique from the schema declaration fixed it.

ran into the same issue & this seems to have fixed it 🤝

Yes, this also solved it for us. However, there is no information in the mongoose SchemaType section explaining "unique"=true creates an index too. Therefore, it would be highly appreciated to get clarification at least in the documentation, ideally with a warning info.

@vkarpov15 vkarpov15 reopened this Dec 26, 2024
@vkarpov15 vkarpov15 modified the milestones: 8.9.2, 8.9.3 Dec 26, 2024
@vkarpov15 vkarpov15 added developer-experience This issue improves error messages, debugging, or reporting and removed confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. labels Dec 26, 2024
@vkarpov15
Copy link
Collaborator

We will change this error into a warning instead to prevent it from blocking anyone from upgrading.

@shehabadel
Copy link

We had the same error for a slightly different scenario:

we had declared the unique keyword twice, once in the schema and second time in a separate index creation:

const schema = new Schema({myId: {type String, required: true, unique: true}})
schema.index({myId: 1}, {unique: true})

removing the unique from the schema declaration fixed it.

This has solved it for us too, thank you 💯

vkarpov15 added a commit that referenced this issue Dec 30, 2024
fix(schema): make duplicate index error a warning for now to prevent blocking upgrading
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
9 participants