Permalink
Browse files

SERVER-8172 Exclude $not query operator fields from new upsert document.

Conflicts:

	src/mongo/dbtests/updatetests.cpp
  • Loading branch information...
1 parent ab40103 commit 8ea5dd424949d66d0859a45e9b5bc0889fb0ca30 @astaple astaple committed with monkey101 Jan 16, 2013
Showing with 43 additions and 3 deletions.
  1. +20 −0 jstests/upsert2.js
  2. +10 −3 src/mongo/db/ops/update_internal.cpp
  3. +13 −0 src/mongo/dbtests/updatetests.cpp
View
@@ -0,0 +1,20 @@
+// A query field with a $not operator should be excluded when constructing the object to which mods
+// will be applied when performing an upsert. SERVER-8178
+
+t = db.jstests_upsert2;
+
+// The a:$not query operator does not cause an 'a' field to be added to the upsert document.
+t.drop();
+t.update( { a:{ $not:{ $lt:1 } } }, { $set:{ b:1 } }, true );
+assert( !t.findOne().a );
+
+// The a:$not query operator does not cause an 'a' field to be added to the upsert document.
+t.drop();
+t.update( { a:{ $not:{ $elemMatch:{ a:1 } } } }, { $set:{ b:1 } }, true );
+assert( !t.findOne().a );
+
+// The a:$not query operator does not cause an 'a' field to be added to the upsert document, and as
+// a result $push can be applied to the (missing) 'a' field.
+t.drop();
+t.update( { a:{ $not:{ $elemMatch:{ a:1 } } } }, { $push:{ a:{ b:1, c:0 } } }, true );
+assert.eq( [ { b:1, c:0 } ], t.findOne().a );
@@ -970,9 +970,16 @@ namespace mongo {
// this can be a query piece
// or can be a dbref or something
- int op = e.embeddedObject().firstElement().getGtLtOp( -1 );
- if ( op >= 0 ) {
- // this means this is a $gt type filter, so don't make part of the new object
+ int op = e.embeddedObject().firstElement().getGtLtOp();
+ if ( op > 0 ) {
+ // This means this is a $gt type filter, so don't make it part of the new
+ // object.
+ continue;
+ }
+
+ if ( str::equals( e.embeddedObject().firstElement().fieldName(), "$not" ) ) {
+ // A $not filter operator is not detected in getGtLtOp() and should not
+ // become part of the new object.
continue;
}
}
@@ -1104,6 +1104,19 @@ namespace UpdateTests {
ASSERT_EQUALS( mod, modSetState->getOpLogRewrite() );
}
};
+
+ class CreateNewFromQueryExcludeNot {
+ public:
+ void run() {
+ BSONObj querySpec = BSON( "a" << BSON( "$not" << BSON( "$lt" << 1 ) ) );
+ BSONObj modSpec = BSON( "$set" << BSON( "b" << 1 ) );
+ ModSet modSet( modSpec );
+
+ // Because a $not operator is applied to the 'a' field, the 'a' field is excluded
+ // from the resulting document.
+ ASSERT_EQUALS( BSON( "b" << 1 ), modSet.createNewFromQuery( querySpec ) );
+ }
+ };
};
namespace basic {

0 comments on commit 8ea5dd4

Please sign in to comment.