Skip to content

Commit 391ecec

Browse files
committedMar 24, 2021
collation not added to text indexes
1 parent 80245ed commit 391ecec

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed
 

‎lib/model.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1575,7 +1575,6 @@ function _ensureIndexes(model, options, callback) {
15751575
let indexError;
15761576

15771577
options = options || {};
1578-
15791578
const done = function(err) {
15801579
if (err && !model.$caught) {
15811580
model.emit('error', err);
@@ -1635,7 +1634,6 @@ function _ensureIndexes(model, options, callback) {
16351634
const indexFields = utils.clone(index[0]);
16361635
const indexOptions = utils.clone(index[1]);
16371636
delete indexOptions._autoIndex;
1638-
16391637
_decorateDiscriminatorIndexOptions(model, indexOptions);
16401638
if ('safe' in options) {
16411639
_handleSafe(options);
@@ -1654,7 +1652,7 @@ function _ensureIndexes(model, options, callback) {
16541652
indexOptions.background = options.background;
16551653
}
16561654
if (model.schema.options.hasOwnProperty('collation') &&
1657-
!indexOptions.hasOwnProperty('collation')) {
1655+
!indexOptions.hasOwnProperty('collation') && Object.values(indexFields) != 'text') {
16581656
indexOptions.collation = model.schema.options.collation;
16591657
}
16601658

‎test/model.indexes.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -666,5 +666,24 @@ describe('model', function() {
666666
]);
667667
});
668668
});
669+
it('should prevent collation on text indexes (gh-10044)', function() {
670+
return co(function*() {
671+
yield db.db.collection('User').drop().catch(() => {});
672+
673+
const userSchema = new mongoose.Schema({ username: String }, {
674+
collation: {
675+
locale: 'en',
676+
strength: 2
677+
}
678+
});
679+
userSchema.index({ username: 'text' }, { unique: true });
680+
const User = db.model('User', userSchema, 'User');
681+
682+
yield User.init();
683+
const indexes = yield User.listIndexes();
684+
assert.ok(!indexes[1].collation);
685+
yield User.collection.drop();
686+
});
687+
});
669688
});
670689
});

0 commit comments

Comments
 (0)
Please sign in to comment.