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

Undocumented breaking change in v7: calling diffIndexes throws error when collection does not exist #14010

Closed
2 tasks done
fjeldstad opened this issue Oct 27, 2023 · 1 comment · Fixed by #14029
Closed
2 tasks done
Assignees
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@fjeldstad
Copy link

fjeldstad commented Oct 27, 2023

Prerequisites

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

Issue

In Mongoose 6, if you called await Model.diffIndexes() on a model where the collection does not yet exist, it would return the index diff and let you proceed with things like await Model.syncIndexes(). I suppose it would create the collection first - but I haven't verified this.

In Mongoose 7, it will throw an error instead:

MongoServerError: ns does not exist [...]

It throws even if you have autoCreate: true set on the global mongoose instance.

I suppose the fix is to call await Model.createCollection() first, but still this was a breaking change that is not listed in the release notes or migration guide as far as I can see.

I created a small demo where you can see the issue here: https://codesandbox.io/p/sandbox/quirky-kapitsa-49vy78?file=%2Findex.js%3A5%2C1

@vkarpov15 vkarpov15 added this to the 7.6.4 milestone Oct 29, 2023
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Oct 29, 2023
@vkarpov15 vkarpov15 modified the milestones: 7.6.4, 7.6.5 Oct 30, 2023
@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Nov 1, 2023
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');

mongoose.set('autoIndex', false);
mongoose.set('autoCreate', true);

const testSchema = new mongoose.Schema({
  name: String
});

const Test = mongoose.model('Test', testSchema);

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();

  const res = await Test.diffIndexes();
  console.log('res', res);
}

run();

@IslandRhythms IslandRhythms linked a pull request Nov 1, 2023 that will close this issue
@vkarpov15 vkarpov15 reopened this Nov 2, 2023
@vkarpov15 vkarpov15 modified the milestones: 7.6.5, 8.0.1 Nov 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants