Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.95k stars 3.84k forks source link

Text indexes get recreated on .syncIndexes() #10850

Closed DmytroMysak closed 3 years ago

DmytroMysak commented 3 years ago

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 commented 3 years ago
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 commented 3 years ago

Your solution was correct