Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SERVER-9790 Add this.$db || db.toString() to DBRef string representation #638

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions jstests/core/constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,18 @@ var dbrefConstructors = {
"valid" : [
"DBRef(\"namespace\", 0)",
"DBRef(\"namespace\", \"test\")",
"DBRef(\"namespace\", \"test\", \"database\")",
"DBRef(\"namespace\", ObjectId())",
"DBRef(\"namespace\", ObjectId(\"000000000000000000000000\"))",
"DBRef(\"namespace\", ObjectId(\"000000000000000000000000\"), \"database\")",
],
"invalid" : [
"DBRef()",
"DBRef(true, ObjectId())",
"DBRef(true, ObjectId(), true)",
"DBRef(\"namespace\")",
"DBRef(\"namespace\", ObjectId(), true)",
"DBRef(\"namespace\", ObjectId(), 123)",
]
}

Expand Down
10 changes: 10 additions & 0 deletions jstests/core/ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ assert( db.things.findOne().o.fetch().n == 1, "dbref broken 2" );
other.n++;
db.otherthings.save(other);
assert( db.things.findOne().o.fetch().n == 2, "dbrefs broken" );

db.getSiblingDB("otherdb").dropDatabase();
var objid = new ObjectId();
db.getSiblingDB("otherdb").getCollection("othercoll").insert({_id:objid, field:"value"});
var subdoc = db.getSiblingDB("otherdb").getCollection("othercoll").findOne({_id:objid})

db.mycoll.drop();
db.mycoll.insert({_id:"asdf", asdf:new DBRef("othercoll", objid, "otherdb")});
var doc = db.mycoll.findOne({_id:"asdf"}, {_id:0, asdf:1});
assert.eq(tojson(doc.asdf.fetch()), tojson(subdoc), "otherdb dbref");
5 changes: 5 additions & 0 deletions jstests/core/shelltypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ b = DBRef(a.getRef(), a.getId());
printjson(a);
assert.eq(tojson(a), tojson(b), "dbref");

a = new DBRef("test", "theid", "testdb");
b = DBRef(a.getRef(), a.getId(), a.getDb());
printjson(a);
assert.eq(tojson(a), tojson(b), "dbref");

a = new DBPointer("test", new ObjectId());
b = DBPointer(a.getCollection(), a.getId());
printjson(a);
Expand Down
8 changes: 7 additions & 1 deletion src/mongo/scripting/v8_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,10 +696,16 @@ namespace mongo {
v8::Handle<v8::Object> it = args.This();
verify(scope->DBRefFT()->HasInstance(it));

argumentCheck(args.Length() == 2, "DBRef needs 2 arguments")
argumentCheck(args.Length() >= 2 && args.Length() <= 3, "DBRef needs 2 or 3 arguments")
argumentCheck(args[0]->IsString(), "DBRef 1st parameter must be a string")
it->ForceSet(scope->v8StringData("$ref"), args[0]);
it->ForceSet(scope->v8StringData("$id"), args[1]);

if (args.Length() == 3) {
argumentCheck(args[2]->IsString(), "DBRef 3rd parameter must be a string")
it->ForceSet(scope->v8StringData("$db"), args[2]);
}

return it;
}

Expand Down
9 changes: 7 additions & 2 deletions src/mongo/shell/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,13 +386,18 @@ if (typeof(DBRef) != "undefined"){
DBRef.prototype.fetch = function(){
assert(this.$ref, "need a ns");
assert(this.$id, "need an id");
return db[ this.$ref ].findOne({ _id : this.$id });
var coll = this.$db ? db.getSiblingDB(this.$db).getCollection(this.$ref) : db[this.$ref];
return coll.findOne({ _id : this.$id }):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

colon at end of line?

}

DBRef.prototype.tojson = function(indent){
return this.toString();
}

DBRef.prototype.getDb = function(){
return this.$db || undefined;
}

DBRef.prototype.getCollection = function(){
return this.$ref;
}
Expand All @@ -406,7 +411,7 @@ if (typeof(DBRef) != "undefined"){
}

DBRef.prototype.toString = function(){
return "DBRef(" + tojson(this.$ref) + ", " + tojson(this.$id) + ")";
return "DBRef(" + tojson(this.$ref) + ", " + tojson(this.$id) + (this.$db ? ", " + tojson(this.$db) : "") + ")";
}
}
else {
Expand Down