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

Text indexes get recreated on .syncIndexes() #10850

Closed
DmytroMysak opened this issue Oct 6, 2021 · 2 comments · Fixed by #10851
Closed

Text indexes get recreated on .syncIndexes() #10850

DmytroMysak opened this issue Oct 6, 2021 · 2 comments · Fixed by #10851
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@DmytroMysak
Copy link

Do you want to request a feature or report a bug?
bug

What is the current behavior?
I'm running .syncIndexes() and the mongoose is recreating text index every time.

If the current behavior is a bug, please provide the steps to reproduce.

const mongoose = require('mongoose');

(async function () {
  const collation = { collation: { locale: 'simple' } };
  const connection = await mongoose.createConnection('mongodb://localhost:27017/test-db').asPromise();
  const someSchema = new mongoose.Schema({
    title: String,
    author: String,
  });
  someSchema.index({ title: 1, author: 'text' }, collation);
  const model = connection.model('Some', someSchema);
  mongoose.set('debug', true);
  await model.syncIndexes({ background: false });

  process.exit(0);
})();

What is the expected behavior?
If the text index is the same, .syncIndexes() shouldn't recreate it every time.

In isIndexEqual we should check db index weights + key (with omiting 2 properties: _fts + _ftsx)
Something like this one:
image

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
node: 14.16.1
mongoose: 6.0.9
MongoDB: 4.4

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Oct 6, 2021
@IslandRhythms
Copy link
Collaborator

IslandRhythms commented Oct 6, 2021

const mongoose = require('mongoose');

(async function () {
  const collation = { collation: { locale: 'simple' } };
  const connection = await mongoose.createConnection('mongodb://localhost:27017/test-db').asPromise();
  
  const someSchema = new mongoose.Schema({
    title: String,
    author: String,
  });
  someSchema.index({ title: 1, author: 'text' }, collation);
  const model = connection.model('Some', someSchema);
  mongoose.set('debug', true);
  await model.syncIndexes({ background: false });
//   await model.syncIndexes({ background: true});
  await model.collection.dropIndexes();
  await mongoose.connection.dropDatabase();
})();

@IslandRhythms
Copy link
Collaborator

Your solution was correct

vkarpov15 added a commit that referenced this issue Oct 8, 2021
fix 10850 by removing two properties from the `dbIndex.key` and assigning whats left to `const weight` with `dbIndex.weights`
@vkarpov15 vkarpov15 added this to the 6.0.10 milestone Oct 8, 2021
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
3 participants