diff --git a/snippets/mongocompat/mongotypes.js b/snippets/mongocompat/mongotypes.js index 8a8ec34..1a0b8ef 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,50 @@ 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}", ${tojson(this.oid)}` + + (this.db ? `, "${this.db}"` : "") + ")"; }; + + Object.defineProperty(DBRef.prototype, "$ref", { + 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 df4b8db..9db374a 100644 --- a/snippets/mongocompat/test.js +++ b/snippets/mongocompat/test.js @@ -70,3 +70,30 @@ 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); +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")');