Skip to content

Commit

Permalink
fix(model): use schema-level default collation for indexes if index d…
Browse files Browse the repository at this point in the history
…oesn't have collation

Fix #9912
  • Loading branch information
vkarpov15 committed Feb 13, 2021
1 parent 003e4a8 commit 6c78829
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1650,6 +1650,10 @@ function _ensureIndexes(model, options, callback) {
if ('background' in options) {
indexOptions.background = options.background;
}
if (model.schema.options.hasOwnProperty('collation') &&
!indexOptions.hasOwnProperty('collation')) {
indexOptions.collation = model.schema.options.collation;
}

const methodName = useCreateIndex ? 'createIndex' : 'ensureIndex';
model.collection[methodName](indexFields, indexOptions, utils.tick(function(err, name) {
Expand Down
1 change: 0 additions & 1 deletion test/model.indexes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,6 @@ describe('model', function() {
let indexes = yield User.listIndexes();
assert.equal(indexes.length, 2);
assert.deepEqual(indexes[1].key, { username: 1 });
console.log(indexes);
assert.ok(indexes[1].collation);
assert.equal(indexes[1].collation.strength, 2);

Expand Down

2 comments on commit 6c78829

@aqemi
Copy link

@aqemi aqemi commented on 6c78829 Mar 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vkarpov15 If there are defined both schema level collation and text index it won't create it:
Index type 'text' does not support collation

@vkarpov15
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@svmn thanks for reporting, I opened a new issue to follow: #10044

Please sign in to comment.