Skip to content

Commit

Permalink
Merge pull request #13659 from Automattic/IslandRhythms/gh-13276
Browse files Browse the repository at this point in the history
enhancement: can index using the alias
  • Loading branch information
vkarpov15 committed Jul 26, 2023
2 parents 370c32d + 7a2adb9 commit 7b39ac6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/schema.js
Expand Up @@ -2027,6 +2027,12 @@ Schema.prototype.index = function(fields, options) {
if (options.expires) {
utils.expires(options);
}
for (const key in fields) {
if (this.aliases[key]) {
fields[this.aliases[key]] = fields[key];
delete fields[key];
}
}

this._indexes.push([fields, options]);
return this;
Expand Down
12 changes: 12 additions & 0 deletions test/schema.alias.test.js
Expand Up @@ -183,4 +183,16 @@ describe('schema alias option', function() {
assert.ok(schema.virtuals['name1']);
assert.ok(schema.virtuals['name2']);
});
it('supports passing the alias name for an index (gh-13276)', function() {
const testSchema = new Schema({
fullName: {
type: String,
alias: 'short'
}
});
testSchema.index({ short: 1 });
const indexes = testSchema.indexes();
assert.equal(indexes[0][0].fullName, 1);

});
});

0 comments on commit 7b39ac6

Please sign in to comment.