Permalink
Browse files

Don't grow a document on $pull/$pullAll. SERVER-6047

  • Loading branch information...
1 parent 7f47f02 commit 7a43c2260ee58f82fc44e55c021b74d821e4a16e @kreuter kreuter committed Jul 2, 2012
Showing with 35 additions and 0 deletions.
  1. +14 −0 jstests/pull.js
  2. +13 −0 jstests/pullall.js
  3. +8 −0 src/mongo/db/ops/update_internal.cpp
View
@@ -17,3 +17,17 @@ t.save( { a: [ 2 ] } );
t.update( {}, { $pull: { a: 2 } } );
t.update( {}, { $pull: { a: 6 } } );
assert.eq( [], t.findOne().a );
+
+// SERVER-6047: $pull creates empty nested docs for dotted fields
+// that don't exist.
+t.drop()
+t.save({ m : 1 } );
+t.update( { m : 1 }, { $pull : { 'a.b' : [ 1 ] } } );
+assert( ('a' in t.findOne()) == false );
+// Non-obvious bit: the implementation of non-in-place update
+// might do different things depending on whether the "new" field
+// comes before or after existing fields in the document.
+// So for now it's worth testing that too. Sorry, future; blame the past.
+t.update( { m : 1 }, { $pull : { 'x.y' : [ 1 ] } } );
+assert( ('z' in t.findOne()) == false );
+// End SERVER-6047
View
@@ -16,3 +16,16 @@ assert.eq( [ 1 ], t.findOne().a );
t.update( {}, { $pullAll: { a: [ 1, 5 ] } } );
assert.eq( [], t.findOne().a );
+// SERVER-6047: $pullAll creates empty nested docs for dotted fields
+// that don't exist.
+t.drop()
+t.save({ m : 1 } );
+t.update( { m : 1 }, { $pullAll : { 'a.b' : [ 1 ] } } );
+assert( ('a' in t.findOne()) == false );
+// Non-obvious bit: the implementation of non-in-place update
+// might do different things depending on whether the "new" field
+// comes before or after existing fields in the document.
+// So for now it's worth testing that too. Sorry, future; blame the past.
+t.update( { m : 1 }, { $pullAll : { 'x.y' : [ 1 ] } } );
+assert( ('z' in t.findOne()) == false );
+// End SERVER-6047
@@ -616,6 +616,14 @@ namespace mongo {
ModState& modState,
BSONBuilderBase& builder,
set<string>& onedownseen ) {
+
+ switch () {
+ // unset/pull/pullAll on nothing does nothing, so don't append anything
+ case Mod::UNSET:
+ case Mod::PULL:
+ case Mod::PULL_ALL:
+ return;
+ }
const char* temp = modState.fieldName();
temp += root.size();
const char* dot = strchr( temp , '.' );

0 comments on commit 7a43c22

Please sign in to comment.