Skip to content

Commit 463f063

Browse files
authored
Merge pull request #14042 from meabed/fix-index-alias
Index: Fix index alias object key position
2 parents b222362 + f6f3d11 commit 463f063

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

lib/schema.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,11 +2037,9 @@ Schema.prototype.index = function(fields, options) {
20372037
}
20382038
for (const key in fields) {
20392039
if (this.aliases[key]) {
2040-
fields[this.aliases[key]] = fields[key];
2041-
delete fields[key];
2040+
fields = utils.renameObjKey(fields, key, this.aliases[key]);
20422041
}
20432042
}
2044-
20452043
for (const field of Object.keys(fields)) {
20462044
if (fields[field] === 'ascending' || fields[field] === 'asc') {
20472045
fields[field] = 1;

lib/utils.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,29 @@ exports.each = function(arr, fn) {
964964
}
965965
};
966966

967+
/**
968+
* Rename an object key, while preserving its position in the object
969+
*
970+
* @param {Object} oldObj
971+
* @param {String|Number} oldKey
972+
* @param {String|Number} newKey
973+
* @api private
974+
*/
975+
exports.renameObjKey = function(oldObj, oldKey, newKey) {
976+
const keys = Object.keys(oldObj);
977+
return keys.reduce(
978+
(acc, val) => {
979+
if (val === oldKey) {
980+
acc[newKey] = oldObj[oldKey];
981+
} else {
982+
acc[val] = oldObj[val];
983+
}
984+
return acc;
985+
},
986+
{}
987+
);
988+
};
989+
967990
/*!
968991
* ignore
969992
*/

0 commit comments

Comments
 (0)