Permalink
Browse files

remove( { x : undefined } ) should not delete everything in the colle…

…ction SERVER-2384
  • Loading branch information...
1 parent 38047f9 commit c31c7f322943034bd294993e832f51383799ee5f @erh erh committed Jan 21, 2011
Showing with 44 additions and 0 deletions.
  1. +2 −0 db/matcher.cpp
  2. +28 −0 jstests/remove_undefined.js
  3. +5 −0 shell/collection.js
  4. +9 −0 shell/mongo_vstudio.cpp
View
2 db/matcher.cpp
@@ -306,6 +306,8 @@ namespace mongo {
BSONObjIterator i(jsobj);
while ( i.more() ) {
BSONElement e = i.next();
+
+ uassert( 13629 , "can't have undefined in a query expression" , e.type() != Undefined );
if ( parseOrNor( e, subMatcher ) ) {
continue;
View
28 jstests/remove_undefined.js
@@ -0,0 +1,28 @@
+
+t = db.drop_undefined.js
+
+t.insert( { _id : 1 } )
+t.insert( { _id : 2 } )
+t.insert( { _id : null } )
+
+z = { foo : 1 , x : null }
+
+t.remove( { x : z.bar } )
+assert.eq( 3 , t.count() , "A1" )
+
+t.remove( { x : undefined } )
+assert.eq( 3 , t.count() , "A2" )
+
+assert.throws( function(){ t.remove( { _id : z.bar } ) } , null , "B1" )
+assert.throws( function(){ t.remove( { _id : undefined } ) } , null , "B2" )
+
+
+t.remove( { _id : z.x } )
+assert.eq( 2 , t.count() , "C1" )
+
+t.insert( { _id : null } )
+assert.eq( 3 , t.count() , "C2" )
+
+assert.throws( function(){ t.remove( { _id : undefined } ) } , null, "C3" )
+assert.eq( 3 , t.count() , "C4" )
+
View
5 shell/collection.js
@@ -174,6 +174,11 @@ DBCollection.prototype.insert = function( obj , _allow_dot ){
}
DBCollection.prototype.remove = function( t , justOne ){
+ for ( var k in t ){
+ if ( k == "_id" && typeof( t[k] ) == "undefined" ){
+ throw "can't have _id set to undefined in a remove expression"
+ }
+ }
this._mongo.remove( this._fullName , this._massageObject( t ) , justOne ? true : false );
}
View
9 shell/mongo_vstudio.cpp
@@ -127,6 +127,10 @@ const StringData _jscode_raw_utils =
"\n"
"assert.throws = function( func , params , msg ){\n"
"if ( assert._debug && msg ) print( \"in assert for: \" + msg );\n"
+"\n"
+"if ( params && typeof( params ) == \"string\" )\n"
+"throw \"2nd argument to assert.throws has to be an array\"\n"
+"\n"
"try {\n"
"func.apply( null , params );\n"
"}\n"
@@ -2940,6 +2944,11 @@ const StringData _jscode_raw_collection =
"}\n"
"\n"
"DBCollection.prototype.remove = function( t , justOne ){\n"
+"for ( var k in t ){\n"
+"if ( k == \"_id\" && typeof( t[k] ) == \"undefined\" ){\n"
+"throw \"can't have _id set to undefined in a remove expression\"\n"
+"}\n"
+"}\n"
"this._mongo.remove( this._fullName , this._massageObject( t ) , justOne ? true : false );\n"
"}\n"
"\n"

0 comments on commit c31c7f3

Please sign in to comment.