From 3c326b6e841a728fbd2130569d51a8f36f6dc6ea Mon Sep 17 00:00:00 2001 From: LijieZhang1998 Date: Mon, 27 Oct 2025 16:47:49 -0500 Subject: [PATCH 1/2] STREAMS-1972: Add old shell compatibility for DBRef for JS Engine --- snippets/mongocompat/mongotypes.js | 36 +++++++++++++++++++++--------- snippets/mongocompat/test.js | 11 +++++++++ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/snippets/mongocompat/mongotypes.js b/snippets/mongocompat/mongotypes.js index 8a8ec34..9ec25c3 100644 --- a/snippets/mongocompat/mongotypes.js +++ b/snippets/mongocompat/mongotypes.js @@ -602,10 +602,10 @@ if (typeof (DBPointer) != "undefined") { // DBRef if (typeof (DBRef) != "undefined") { DBRef.prototype.fetch = function() { - assert(this.$ref, "need a ns"); - assert(this.$id, "need an id"); - var coll = this.$db ? db.getSiblingDB(this.$db).getCollection(this.$ref) : db[this.$ref]; - return coll.findOne({_id: this.$id}); + assert(this.collection, "need a ns"); + assert(this.oid, "need an id"); + var coll = this.db ? db.getSiblingDB(this.db).getCollection(this.collection) : db[this.collection]; + return coll.findOne({_id: this.oid}); }; DBRef.prototype.tojson = function(indent) { @@ -613,25 +613,41 @@ if (typeof (DBRef) != "undefined") { }; DBRef.prototype.getDb = function() { - return this.$db || undefined; + return this.db || undefined; }; DBRef.prototype.getCollection = function() { - return this.$ref; + return this.collection; }; DBRef.prototype.getRef = function() { - return this.$ref; + return this.collection; }; DBRef.prototype.getId = function() { - return this.$id; + return this.oid; }; DBRef.prototype.toString = function() { - return "DBRef(" + tojson(this.$ref) + ", " + tojson(this.$id) + - (this.$db ? ", " + tojson(this.$db) : "") + ")"; + return `DBRef("${this.collection}", ${this.oid.tojson()}` + + (this.db ? `, "${this.db}"` : "") + ")"; }; + + Object.defineProperty(DBRef.prototype, "$ref", { + get: function () { + return this.collection; + }, + }); + Object.defineProperty(DBRef.prototype, "$id", { + get: function () { + return this.oid; + }, + }); + Object.defineProperty(DBRef.prototype, "$db", { + get: function () { + return this.db; + }, + }); } else { print("warning: no DBRef"); } diff --git a/snippets/mongocompat/test.js b/snippets/mongocompat/test.js index df4b8db..50d7f04 100644 --- a/snippets/mongocompat/test.js +++ b/snippets/mongocompat/test.js @@ -70,3 +70,14 @@ assert.strictEqual(tsFromStr.i, 255); assert.strictEqual(tsFromStr.t, 0); assert.strictEqual(Timestamp.MAX_VALUE._bsontype, 'Long'); assert.strictEqual(Timestamp.MAX_VALUE, Long.MAX_UNSIGNED_VALUE); + +const id = ObjectId('68ffa28b77bba38c9ddcf376'); +const dbRef = DBRef('testColl', id, 'testDb'); +assert.strictEqual(dbRef.toString(), 'DBRef("testColl", ObjectId("68ffa28b77bba38c9ddcf376"), "testDb")'); +assert.strictEqual(dbRef.tojson(), 'DBRef("testColl", ObjectId("68ffa28b77bba38c9ddcf376"), "testDb")'); +assert.strictEqual(dbRef.$ref, 'testColl'); +assert.strictEqual(dbRef.$id, id); +assert.strictEqual(dbRef.$db, 'testDb'); +const dbRefNoDb = DBRef('testColl', id); +assert.strictEqual(dbRefNoDb.toString(), 'DBRef("testColl", ObjectId("68ffa28b77bba38c9ddcf376"))'); +assert.strictEqual(dbRefNoDb.$db, undefined); From 15646ba7f10361a68baa1edc835c1bb970cacb68 Mon Sep 17 00:00:00 2001 From: LijieZhang1998 Date: Mon, 27 Oct 2025 21:12:47 -0500 Subject: [PATCH 2/2] Refactor by comments --- snippets/mongocompat/mongotypes.js | 11 ++++++++++- snippets/mongocompat/test.js | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/snippets/mongocompat/mongotypes.js b/snippets/mongocompat/mongotypes.js index 9ec25c3..1a0b8ef 100644 --- a/snippets/mongocompat/mongotypes.js +++ b/snippets/mongocompat/mongotypes.js @@ -629,7 +629,7 @@ if (typeof (DBRef) != "undefined") { }; DBRef.prototype.toString = function() { - return `DBRef("${this.collection}", ${this.oid.tojson()}` + + return `DBRef("${this.collection}", ${tojson(this.oid)}` + (this.db ? `, "${this.db}"` : "") + ")"; }; @@ -637,16 +637,25 @@ if (typeof (DBRef) != "undefined") { get: function () { return this.collection; }, + set: function (value) { + this.collection = value; + }, }); Object.defineProperty(DBRef.prototype, "$id", { get: function () { return this.oid; }, + set: function (value) { + this.oid = value; + }, }); Object.defineProperty(DBRef.prototype, "$db", { get: function () { return this.db; }, + set: function (value) { + this.db = value; + }, }); } else { print("warning: no DBRef"); diff --git a/snippets/mongocompat/test.js b/snippets/mongocompat/test.js index 50d7f04..9db374a 100644 --- a/snippets/mongocompat/test.js +++ b/snippets/mongocompat/test.js @@ -81,3 +81,19 @@ assert.strictEqual(dbRef.$db, 'testDb'); const dbRefNoDb = DBRef('testColl', id); assert.strictEqual(dbRefNoDb.toString(), 'DBRef("testColl", ObjectId("68ffa28b77bba38c9ddcf376"))'); assert.strictEqual(dbRefNoDb.$db, undefined); +const dbRefStringId = DBRef('testColl', '68ffa28b77bba38c9ddcf376'); +assert.strictEqual(dbRefStringId.toString(), 'DBRef("testColl", "68ffa28b77bba38c9ddcf376")'); +const dbRefForSetters = DBRef('originalColl', id, 'originalDb'); +dbRefForSetters.$ref = 'newColl'; +assert.strictEqual(dbRefForSetters.$ref, 'newColl'); +assert.strictEqual(dbRefForSetters.collection, 'newColl'); +assert.strictEqual(dbRefForSetters.toString(), 'DBRef("newColl", ObjectId("68ffa28b77bba38c9ddcf376"), "originalDb")'); +const newId = ObjectId('507f1f77bcf86cd799439011'); +dbRefForSetters.$id = newId; +assert.strictEqual(dbRefForSetters.$id, newId); +assert.strictEqual(dbRefForSetters.oid, newId); +assert.strictEqual(dbRefForSetters.toString(), 'DBRef("newColl", ObjectId("507f1f77bcf86cd799439011"), "originalDb")'); +dbRefForSetters.$db = 'newDb'; +assert.strictEqual(dbRefForSetters.$db, 'newDb'); +assert.strictEqual(dbRefForSetters.db, 'newDb'); +assert.strictEqual(dbRefForSetters.toString(), 'DBRef("newColl", ObjectId("507f1f77bcf86cd799439011"), "newDb")');