Skip to content

Commit

Permalink
SERVER-652 unset/array sets value to null, which makes more sense
Browse files Browse the repository at this point in the history
  • Loading branch information
astaple committed Mar 3, 2010
1 parent 09b1285 commit 4461c46
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
8 changes: 6 additions & 2 deletions db/jsobj.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1664,11 +1664,15 @@ namespace mongo {
_b.appendAs(e, num().c_str()); _b.appendAs(e, num().c_str());
return *this; return *this;
} }

template <typename T> template <typename T>
BSONArrayBuilder& operator<<(const T& x){ BSONArrayBuilder& operator<<(const T& x){
return append(x); return append(x);
} }

void appendNull() {
_b.appendNull(num().c_str());
}


BSONArray arr(){ return BSONArray(_b.obj()); } BSONArray arr(){ return BSONArray(_b.obj()); }


Expand Down Expand Up @@ -1705,7 +1709,7 @@ namespace mongo {
void fill( const char *name ) { void fill( const char *name ) {
char *r; char *r;
int n = strtol( name, &r, 10 ); 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 ) while( _i < n )
append( nullElt() ); append( nullElt() );
} }
Expand Down
11 changes: 10 additions & 1 deletion db/update.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ namespace mongo {
ms.appendIncValue( bb ); ms.appendIncValue( bb );
} }


template< class Builder >
void appendUnset( Builder &b ) {
}

template<>
void appendUnset( BSONArrayBuilder &b ) {
b.appendNull();
}

template< class Builder > template< class Builder >
void Mod::apply( Builder& b , BSONElement in , ModState& ms ) const { void Mod::apply( Builder& b , BSONElement in , ModState& ms ) const {
switch ( op ){ switch ( op ){
Expand All @@ -83,7 +92,7 @@ namespace mongo {
} }


case UNSET: { case UNSET: {
//Explicit NOOP appendUnset( b );
break; break;
} }


Expand Down
16 changes: 12 additions & 4 deletions jstests/unset2.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ t.drop();


t.save( {a:["a","b","c","d"]} ); t.save( {a:["a","b","c","d"]} );
t.update( {}, {$unset:{"a.3":1}} ); 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}} ); 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}} ); 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.drop();
t.save( {a:["a","b","c","d","e"]} ); t.save( {a:["a","b","c","d","e"]} );
t.update( {}, {$unset:{"a.2":1},$set:{"a.3":3,"a.4":4,"a.5":5}} ); 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.