Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Array.fetchRefs SERVER-213

  • Loading branch information...
commit 29069c485d9fd64e9a67d8f4f59181ad7b2a1737 1 parent 727fc30
@erh erh authored
Showing with 45 additions and 1 deletion.
  1. +23 −0 jstests/ref4.js
  2. +22 −1 shell/utils.js
View
23 jstests/ref4.js
@@ -0,0 +1,23 @@
+
+a = db.ref4a;
+b = db.ref4b;
+
+a.drop();
+b.drop();
+
+db.otherthings.drop();
+db.things.drop();
+
+var other = { s : "other thing", n : 17 };
+b.save(other);
+
+a.save( { name : "abc" , others : [ new DBRef( "ref4b" , other._id ) , new DBPointer( "ref4b" , other._id ) ] } );
+assert( a.findOne().others[0].fetch().n == 17 , "dbref broken 1" );
+
+x = Array.fetchRefs( a.findOne().others );
+assert.eq( 2 , x.length , "A" );
+assert.eq( 17 , x[0].n , "B" );
+assert.eq( 17 , x[1].n , "C" );
+
+
+assert.eq( 0 , Array.fetchRefs( a.findOne().others , "z" ).length , "D" );
View
23 shell/utils.js
@@ -224,6 +224,19 @@ Array.tojson = function( a , sepLines ){
return s;
}
+Array.fetchRefs = function( arr , coll ){
+ var n = [];
+ for ( var i=0; i<arr.length; i ++){
+ var z = arr[i];
+ if ( coll && coll != z.getCollection() )
+ continue;
+ n.push( z.fetch() );
+ }
+
+ return n;
+}
+
+
if ( ! ObjectId.prototype )
ObjectId.prototype = {}
@@ -248,6 +261,10 @@ if ( typeof( DBPointer ) != "undefined" ){
DBPointer.prototype.tojson = function(){
return "{ 'ns' : \"" + this.ns + "\" , 'id' : \"" + this.id + "\" } ";
}
+
+ DBPointer.prototype.getCollection = function(){
+ return this.ns;
+ }
DBPointer.prototype.toString = function(){
return "DBPointer " + this.ns + ":" + this.id;
@@ -266,7 +283,11 @@ if ( typeof( DBRef ) != "undefined" ){
}
DBRef.prototype.tojson = function(){
- return "{ '$ref' : \"" + this.ns + "\" , '$id' : \"" + this.id + "\" } ";
+ return "{ '$ref' : \"" + this.$ref + "\" , '$id' : \"" + this.$id + "\" } ";
+ }
+
+ DBRef.prototype.getCollection = function(){
+ return this.$ref;
}
DBRef.prototype.toString = function(){
Please sign in to comment.
Something went wrong with that request. Please try again.