Permalink
Browse files

SERVER-652 unset/array sets value to null, which makes more sense

  • Loading branch information...
1 parent 09b1285 commit 4461c46f3f34a8a5c4d24cfb0485e09014135af5 @astaple astaple committed Mar 3, 2010
Showing with 28 additions and 7 deletions.
  1. +6 −2 db/jsobj.h
  2. +10 −1 db/update.cpp
  3. +12 −4 jstests/unset2.js
View
8 db/jsobj.h
@@ -1664,11 +1664,15 @@ namespace mongo {
_b.appendAs(e, num().c_str());
return *this;
}
-
+
template <typename T>
BSONArrayBuilder& operator<<(const T& x){
return append(x);
}
+
+ void appendNull() {
+ _b.appendNull(num().c_str());
+ }
BSONArray arr(){ return BSONArray(_b.obj()); }
@@ -1705,7 +1709,7 @@ namespace mongo {
void fill( const char *name ) {
char *r;
int n = strtol( name, &r, 10 );
- uassert( 13047, "can't append to array using string field name", !*r );
+ uassert( 13048, "can't append to array using string field name", !*r );
while( _i < n )
append( nullElt() );
}
View
11 db/update.cpp
@@ -68,6 +68,15 @@ namespace mongo {
}
template< class Builder >
+ void appendUnset( Builder &b ) {
+ }
+
+ template<>
+ void appendUnset( BSONArrayBuilder &b ) {
+ b.appendNull();
+ }
+
+ template< class Builder >
void Mod::apply( Builder& b , BSONElement in , ModState& ms ) const {
switch ( op ){
@@ -83,7 +92,7 @@ namespace mongo {
}
case UNSET: {
- //Explicit NOOP
+ appendUnset( b );
break;
}
View
16 jstests/unset2.js
@@ -3,13 +3,21 @@ t.drop();
t.save( {a:["a","b","c","d"]} );
t.update( {}, {$unset:{"a.3":1}} );
-assert.eq( ["a","b","c"], t.findOne().a );
+assert.eq( ["a","b","c",null], t.findOne().a );
t.update( {}, {$unset:{"a.1":1}} );
-assert.eq( ["a","c"], t.findOne().a );
+assert.eq( ["a",null,"c",null], t.findOne().a );
t.update( {}, {$unset:{"a.0":1}} );
-assert.eq( ["c"], t.findOne().a );
+assert.eq( [null,null,"c",null], t.findOne().a );
+t.update( {}, {$unset:{"a.4":1}} );
+assert.eq( [null,null,"c",null], t.findOne().a ); // no change
t.drop();
t.save( {a:["a","b","c","d","e"]} );
t.update( {}, {$unset:{"a.2":1},$set:{"a.3":3,"a.4":4,"a.5":5}} );
-assert.eq( ["a","b",3,4,5], t.findOne().a );
+assert.eq( ["a","b",null,3,4,5], t.findOne().a );
+
+t.drop();
+t.save( {a:["a","b","c","d","e"]} );
+t.update( {}, {$unset:{"a.2":1},$set:{"a.2":4}} );
+assert( db.getLastError() );
+assert.eq( ["a","b","c","d","e"], t.findOne().a );

0 comments on commit 4461c46

Please sign in to comment.