summaryrefslogtreecommitdiff
path: root/src/mongo/shell/types.js
diff options
context:
space:
mode:
authorLucas de Castro Borges <lucas@gnuabordo.com.br>2025-02-18 17:02:53 -0300
committerLucas de Castro Borges <lucas@gnuabordo.com.br>2025-02-18 17:02:53 -0300
commit959575a5ca598bf5f37fb5cebe7ed1d80d3d71f7 (patch)
treeacc8d60aedb12b70048e676e8a7349deb0010db8 /src/mongo/shell/types.js
parent76588293975fc059cf076779e4283e6ffaf8afff (diff)
New upstream version 6.0.20upstream
Diffstat (limited to 'src/mongo/shell/types.js')
-rw-r--r--src/mongo/shell/types.js30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/mongo/shell/types.js b/src/mongo/shell/types.js
index c06601f9161..ce8fd02b172 100644
--- a/src/mongo/shell/types.js
+++ b/src/mongo/shell/types.js
@@ -292,6 +292,36 @@ Object.merge = function(dst, src, deep) {
return Object.extend(clone, src, deep);
};
+// If there is a conflict in values of a key for two objects being merged, the second value will
+// override the first one in the merged object
+Object.deepMerge = function(...objects) {
+ const isObject = obj => obj && typeof obj === 'object';
+
+ // Create new object prev to hold combination of all object fields.
+ return objects.reduce((prev, obj) => {
+ if (obj === undefined) {
+ obj = {};
+ }
+ Object.keys(obj).forEach(key => {
+ const pVal = prev[key]; // Get the values for key from the two objects being merged.
+ const oVal = obj[key];
+
+ if (Array.isArray(pVal) &&
+ Array.isArray(oVal)) { // If both are arrays then concatenate them into a new
+ // array and add it to prev.
+ prev[key] = pVal.concat(...oVal);
+ } else if (isObject(pVal) &&
+ isObject(oVal)) { // If both are objects then recursively merge again.
+ prev[key] = Object.deepMerge(pVal, oVal);
+ } else { // In all other cases set prev[key] to obj[key].
+ prev[key] = oVal;
+ }
+ });
+
+ return prev;
+ }, {});
+};
+
Object.keySet = function(o) {
var ret = new Array();
for (var i in o) {