Skip to content

Commit

Permalink
Merge pull request #10063 from Automattic/gh-10044
Browse files Browse the repository at this point in the history
collation not added to text indexes
  • Loading branch information
vkarpov15 committed Mar 31, 2021
2 parents f24953c + 9e4a083 commit 966770f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/model.js
Expand Up @@ -1575,7 +1575,6 @@ function _ensureIndexes(model, options, callback) {
let indexError;

options = options || {};

const done = function(err) {
if (err && !model.$caught) {
model.emit('error', err);
Expand Down Expand Up @@ -1634,8 +1633,13 @@ function _ensureIndexes(model, options, callback) {

const indexFields = utils.clone(index[0]);
const indexOptions = utils.clone(index[1]);
let isTextIndex = false;
for (const key of Object.keys(indexFields)) {
if (indexFields[key] === 'text') {
isTextIndex = true;
}
}
delete indexOptions._autoIndex;

_decorateDiscriminatorIndexOptions(model, indexOptions);
if ('safe' in options) {
_handleSafe(options);
Expand All @@ -1654,7 +1658,8 @@ function _ensureIndexes(model, options, callback) {
indexOptions.background = options.background;
}
if (model.schema.options.hasOwnProperty('collation') &&
!indexOptions.hasOwnProperty('collation')) {
!indexOptions.hasOwnProperty('collation') &&
!isTextIndex) {
indexOptions.collation = model.schema.options.collation;
}

Expand Down
17 changes: 17 additions & 0 deletions test/model.indexes.test.js
Expand Up @@ -666,5 +666,22 @@ describe('model', function() {
]);
});
});
it('should prevent collation on text indexes (gh-10044)', function() {
return co(function*() {
const userSchema = new mongoose.Schema({ username: String }, {
collation: {
locale: 'en',
strength: 2
}
});
userSchema.index({ username: 'text' }, { unique: true });
const User = db.model('User', userSchema, 'User');

yield User.init();
const indexes = yield User.listIndexes();
assert.ok(!indexes[1].collation);
yield User.collection.drop();
});
});
});
});

0 comments on commit 966770f

Please sign in to comment.