diff --git a/lib/helpers/clone.js b/lib/helpers/clone.js index fafb2f3063b..b0e37f6cfba 100644 --- a/lib/helpers/clone.js +++ b/lib/helpers/clone.js @@ -17,7 +17,7 @@ const trustedSymbol = require('./query/trusted').trustedSymbol; * * If options.minimize is true, creates a minimal data object. Empty objects and undefined values will not be cloned. This makes the data payload sent to MongoDB as small as possible. * - * Functions are never cloned. + * Functions and primitives are never cloned. * * @param {Object} obj the object to clone * @param {Object} options @@ -30,6 +30,9 @@ function clone(obj, options, isArrayChild) { if (obj == null) { return obj; } + if (typeof obj === 'number' || typeof obj === 'string' || typeof obj === 'boolean' || typeof obj === 'bigint') { + return obj; + } if (Array.isArray(obj)) { return cloneArray(isMongooseArray(obj) ? obj.__array : obj, options); @@ -148,13 +151,12 @@ function cloneObject(obj, options, isArrayChild) { ret[trustedSymbol] = obj[trustedSymbol]; } - let i = 0; - let key = ''; const keys = Object.keys(obj); const len = keys.length; - for (i = 0; i < len; ++i) { - if (specialProperties.has(key = keys[i])) { + for (let i = 0; i < len; ++i) { + const key = keys[i]; + if (specialProperties.has(key)) { continue; }