Skip to content

Commit 3c326b6

Browse files
STREAMS-1972: Add old shell compatibility for DBRef for JS Engine
1 parent 7f418ce commit 3c326b6

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

snippets/mongocompat/mongotypes.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -602,36 +602,52 @@ if (typeof (DBPointer) != "undefined") {
602602
// DBRef
603603
if (typeof (DBRef) != "undefined") {
604604
DBRef.prototype.fetch = function() {
605-
assert(this.$ref, "need a ns");
606-
assert(this.$id, "need an id");
607-
var coll = this.$db ? db.getSiblingDB(this.$db).getCollection(this.$ref) : db[this.$ref];
608-
return coll.findOne({_id: this.$id});
605+
assert(this.collection, "need a ns");
606+
assert(this.oid, "need an id");
607+
var coll = this.db ? db.getSiblingDB(this.db).getCollection(this.collection) : db[this.collection];
608+
return coll.findOne({_id: this.oid});
609609
};
610610

611611
DBRef.prototype.tojson = function(indent) {
612612
return this.toString();
613613
};
614614

615615
DBRef.prototype.getDb = function() {
616-
return this.$db || undefined;
616+
return this.db || undefined;
617617
};
618618

619619
DBRef.prototype.getCollection = function() {
620-
return this.$ref;
620+
return this.collection;
621621
};
622622

623623
DBRef.prototype.getRef = function() {
624-
return this.$ref;
624+
return this.collection;
625625
};
626626

627627
DBRef.prototype.getId = function() {
628-
return this.$id;
628+
return this.oid;
629629
};
630630

631631
DBRef.prototype.toString = function() {
632-
return "DBRef(" + tojson(this.$ref) + ", " + tojson(this.$id) +
633-
(this.$db ? ", " + tojson(this.$db) : "") + ")";
632+
return `DBRef("${this.collection}", ${this.oid.tojson()}` +
633+
(this.db ? `, "${this.db}"` : "") + ")";
634634
};
635+
636+
Object.defineProperty(DBRef.prototype, "$ref", {
637+
get: function () {
638+
return this.collection;
639+
},
640+
});
641+
Object.defineProperty(DBRef.prototype, "$id", {
642+
get: function () {
643+
return this.oid;
644+
},
645+
});
646+
Object.defineProperty(DBRef.prototype, "$db", {
647+
get: function () {
648+
return this.db;
649+
},
650+
});
635651
} else {
636652
print("warning: no DBRef");
637653
}

snippets/mongocompat/test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,14 @@ assert.strictEqual(tsFromStr.i, 255);
7070
assert.strictEqual(tsFromStr.t, 0);
7171
assert.strictEqual(Timestamp.MAX_VALUE._bsontype, 'Long');
7272
assert.strictEqual(Timestamp.MAX_VALUE, Long.MAX_UNSIGNED_VALUE);
73+
74+
const id = ObjectId('68ffa28b77bba38c9ddcf376');
75+
const dbRef = DBRef('testColl', id, 'testDb');
76+
assert.strictEqual(dbRef.toString(), 'DBRef("testColl", ObjectId("68ffa28b77bba38c9ddcf376"), "testDb")');
77+
assert.strictEqual(dbRef.tojson(), 'DBRef("testColl", ObjectId("68ffa28b77bba38c9ddcf376"), "testDb")');
78+
assert.strictEqual(dbRef.$ref, 'testColl');
79+
assert.strictEqual(dbRef.$id, id);
80+
assert.strictEqual(dbRef.$db, 'testDb');
81+
const dbRefNoDb = DBRef('testColl', id);
82+
assert.strictEqual(dbRefNoDb.toString(), 'DBRef("testColl", ObjectId("68ffa28b77bba38c9ddcf376"))');
83+
assert.strictEqual(dbRefNoDb.$db, undefined);

0 commit comments

Comments
 (0)