diff --git a/lib/schema.js b/lib/schema.js index 852851b2265..a13ce57ebf3 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -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; diff --git a/test/schema.alias.test.js b/test/schema.alias.test.js index 0f37f595c1a..2c0977a4206 100644 --- a/test/schema.alias.test.js +++ b/test/schema.alias.test.js @@ -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); + + }); });