Skip to content

Commit

Permalink
enhancement: can index using the alias
Browse files Browse the repository at this point in the history
  • Loading branch information
IslandRhythms committed Jul 26, 2023
1 parent 370c32d commit 7a2adb9
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 7a2adb9

Please sign in to comment.