From 2dbdab54f945c1b24931fd3853bffbfc5a409c11 Mon Sep 17 00:00:00 2001 From: Wernfried Date: Thu, 12 Sep 2024 13:07:41 +0200 Subject: [PATCH 1/6] Support MinKey and MaxKey Added support for MinKey and MaxKey in tojson() --- snippets/mongocompat/mongotypes.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/mongocompat/mongotypes.js b/snippets/mongocompat/mongotypes.js index e790fb2..2990b22 100644 --- a/snippets/mongocompat/mongotypes.js +++ b/snippets/mongocompat/mongotypes.js @@ -586,8 +586,8 @@ tojson = function(x, indent, nolint, depth) { return s; } case "function": - if (x === MinKey || x === MaxKey) - return x.tojson(); + if (x === MinKey) return tojson({ "$minKey" : 1 }); + if (x === MaxKey) return tojson({ "$maxKey" : 1 }); return x.toString(); default: throw Error("tojson can't handle type " + (typeof x)); From a6b4edbb9787afaba809612bb22e3059d16ae877 Mon Sep 17 00:00:00 2001 From: Wernfried Date: Thu, 12 Sep 2024 13:11:49 +0200 Subject: [PATCH 2/6] Check if 'str' property already exist Property should not be defined if already existing --- snippets/mongocompat/mongotypes.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/snippets/mongocompat/mongotypes.js b/snippets/mongocompat/mongotypes.js index 2990b22..ad8038b 100644 --- a/snippets/mongocompat/mongotypes.js +++ b/snippets/mongocompat/mongotypes.js @@ -414,12 +414,14 @@ ObjectId.prototype.tojson = function() { return this.toString(); }; -Object.defineProperty(ObjectId.prototype, 'str', { - enumerable: true, - get() { - return this.toHexString(); - } -}); +if (!ObjectId.prototype.hasOwnProperty('str')) { + Object.defineProperty(ObjectId.prototype, 'str', { + enumerable: true, + get() { + return this.toHexString(); + } + }); +} ObjectId.prototype.valueOf = function() { return this.str; From c4775012220d1e4604e27bc428834cf3f8aa5cf4 Mon Sep 17 00:00:00 2001 From: Wernfried Date: Thu, 12 Sep 2024 13:30:17 +0200 Subject: [PATCH 3/6] Removed ISODate() Function not needed anymore with mongsh 2.3.1. See https://jira.mongodb.org/browse/MONGOSH-1859 --- snippets/mongocompat/mongotypes.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/snippets/mongocompat/mongotypes.js b/snippets/mongocompat/mongotypes.js index ad8038b..95c4ea2 100644 --- a/snippets/mongocompat/mongotypes.js +++ b/snippets/mongocompat/mongotypes.js @@ -75,7 +75,9 @@ Date.prototype.tojson = function() { ofs + '")'; }; -ISODate = function(isoDateStr) { +if (buildInfo().version.split('.').reverse().map((x,i) => parseInt(x) * Math.pow(10,3*i)).reduce((sum, x) => sum + x, 0) < 2003001) { + // Not needed anymore in version 2.3.1 or later + ISODate = function(isoDateStr) { if (!isoDateStr) return new Date(); @@ -121,7 +123,8 @@ ISODate = function(isoDateStr) { throw Error("invalid ISO date: " + isoDateStr); return new Date(time); -}; + }; +} // Regular Expression RegExp.escape = function(text) { From a956e1172dc329a2664bb5c7ce9ee1f03eceea4c Mon Sep 17 00:00:00 2001 From: Wernfried Date: Thu, 12 Sep 2024 13:38:19 +0200 Subject: [PATCH 4/6] Better format in tojsonObject --- snippets/mongocompat/mongotypes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/mongocompat/mongotypes.js b/snippets/mongocompat/mongotypes.js index 95c4ea2..1b93c8b 100644 --- a/snippets/mongocompat/mongotypes.js +++ b/snippets/mongocompat/mongotypes.js @@ -653,7 +653,7 @@ tojsonObject = function(x, indent, nolint, depth) { if (typeof DBCollection != 'undefined' && val == DBCollection.prototype) continue; - fieldStrings.push(indent + "\"" + k + "\" : " + tojson(val, indent, nolint, depth + 1)); + fieldStrings.push(indent + k + ": " + tojson(val, indent, nolint, depth + 1)); } if (fieldStrings.length > 0) { From 26702f6b4d82d6376262942ee1790be990b52b7d Mon Sep 17 00:00:00 2001 From: Wernfried Date: Thu, 12 Sep 2024 13:40:47 +0200 Subject: [PATCH 5/6] Revert "Better format in tojsonObject" This reverts commit a956e1172dc329a2664bb5c7ce9ee1f03eceea4c. --- snippets/mongocompat/mongotypes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/mongocompat/mongotypes.js b/snippets/mongocompat/mongotypes.js index 1b93c8b..95c4ea2 100644 --- a/snippets/mongocompat/mongotypes.js +++ b/snippets/mongocompat/mongotypes.js @@ -653,7 +653,7 @@ tojsonObject = function(x, indent, nolint, depth) { if (typeof DBCollection != 'undefined' && val == DBCollection.prototype) continue; - fieldStrings.push(indent + k + ": " + tojson(val, indent, nolint, depth + 1)); + fieldStrings.push(indent + "\"" + k + "\" : " + tojson(val, indent, nolint, depth + 1)); } if (fieldStrings.length > 0) { From 7c83f9889abe8e4ecede1b3eac6836c1a657fed8 Mon Sep 17 00:00:00 2001 From: Wernfried Date: Wed, 18 Sep 2024 09:00:30 +0200 Subject: [PATCH 6/6] Update mongotypes.js --- snippets/mongocompat/mongotypes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/mongocompat/mongotypes.js b/snippets/mongocompat/mongotypes.js index 95c4ea2..1b93c8b 100644 --- a/snippets/mongocompat/mongotypes.js +++ b/snippets/mongocompat/mongotypes.js @@ -653,7 +653,7 @@ tojsonObject = function(x, indent, nolint, depth) { if (typeof DBCollection != 'undefined' && val == DBCollection.prototype) continue; - fieldStrings.push(indent + "\"" + k + "\" : " + tojson(val, indent, nolint, depth + 1)); + fieldStrings.push(indent + k + ": " + tojson(val, indent, nolint, depth + 1)); } if (fieldStrings.length > 0) {