diff options
author | Alberto Lerner <alerner@10gen.com> | 2013-01-03 09:19:13 -0800 |
---|---|---|
committer | Alberto Lerner <alerner@10gen.com> | 2013-01-03 09:19:31 -0800 |
commit | b3687e73545f6d0d04f1992119308e7ab80d44e1 (patch) | |
tree | e59a4dd2c209ff538c0cdfdbeee13c409899189f /src/mongo/dbtests/updatetests.cpp | |
parent | 234fabfd27e8732b1fe42e2f885f64371de5a774 (diff) | |
download | mongo-b3687e73545f6d0d04f1992119308e7ab80d44e1.tar.gz |
SERVER-991 SERVER-8008 Replaced $trimTo by $slice (but only accepting negative slices for now).
Diffstat (limited to 'src/mongo/dbtests/updatetests.cpp')
-rw-r--r-- | src/mongo/dbtests/updatetests.cpp | 472 |
1 files changed, 276 insertions, 196 deletions
diff --git a/src/mongo/dbtests/updatetests.cpp b/src/mongo/dbtests/updatetests.cpp index 0cf177ace91..f3a951899be 100644 --- a/src/mongo/dbtests/updatetests.cpp +++ b/src/mongo/dbtests/updatetests.cpp @@ -461,219 +461,219 @@ namespace UpdateTests { } }; - class PushTrimBelowFull : public SetBase { + class PushSliceBelowFull : public SetBase { public: void run() { client().insert( ns(), fromjson( "{'_id':0,a:[1]}" ) ); - // { $push : { a : { $each : [ 2 ] , $trimTo : 3 } } } - BSONObj pushObj = BSON( "$each" << BSON_ARRAY( 2 ) << "$trimTo" << 3 ); + // { $push : { a : { $each : [ 2 ] , $slice : -3 } } } + BSONObj pushObj = BSON( "$each" << BSON_ARRAY( 2 ) << "$slice" << -3 ); client().update( ns(), Query(), BSON( "$push" << BSON( "a" << pushObj ) ) ); ASSERT_EQUALS( client().findOne( ns(), Query() ) , fromjson( "{'_id':0,a:[1,2]}" ) ); } }; - class PushTrimReachedFullExact : public SetBase { + class PushSliceReachedFullExact : public SetBase { public: void run() { client().insert( ns(), fromjson( "{'_id':0,a:[1]}" ) ); - // { $push : { a : { $each : [ 2 ] , $trimTo : 2 } } } - BSONObj pushObj = BSON( "$each" << BSON_ARRAY( 2 ) << "$trimTo" << 2 ); + // { $push : { a : { $each : [ 2 ] , $slice : -2 } } } + BSONObj pushObj = BSON( "$each" << BSON_ARRAY( 2 ) << "$slice" << -2 ); client().update( ns(), Query(), BSON( "$push" << BSON( "a" << pushObj ) ) ); ASSERT_EQUALS( client().findOne( ns(), Query() ) , fromjson( "{'_id':0,a:[1,2]}" ) ); } }; - class PushTrimReachedFullWithEach : public SetBase { + class PushSliceReachedFullWithEach : public SetBase { public: void run() { client().insert( ns(), fromjson( "{'_id':0,a:[1]}" ) ); - // { $push : { a : { $each : [ 2 , 3 ] , $trimTo : 2 } } } - BSONObj pushObj = BSON( "$each" << BSON_ARRAY( 2 << 3 ) << "$trimTo" << 2 ); + // { $push : { a : { $each : [ 2 , 3 ] , $slice : -2 } } } + BSONObj pushObj = BSON( "$each" << BSON_ARRAY( 2 << 3 ) << "$slice" << -2 ); client().update( ns(), Query(), BSON( "$push" << BSON( "a" << pushObj ) ) ); ASSERT_EQUALS( client().findOne( ns(), Query() ) , fromjson( "{'_id':0,a:[2,3]}" ) ); } }; - class PushTrimReachedFullWithBoth : public SetBase { + class PushSliceReachedFullWithBoth : public SetBase { public: void run() { client().insert( ns(), fromjson( "{'_id':0,a:[1,2]}" ) ); - // { $push : { a : { $each : [ 3 ] , $trimTo : 2 } } } - BSONObj pushObj = BSON( "$each" << BSON_ARRAY( 3 ) << "$trimTo" << 2 ); + // { $push : { a : { $each : [ 3 ] , $slice : -2 } } } + BSONObj pushObj = BSON( "$each" << BSON_ARRAY( 3 ) << "$slice" << -2 ); client().update( ns(), Query(), BSON( "$push" << BSON( "a" << pushObj ) ) ); ASSERT_EQUALS( client().findOne( ns(), Query() ) , fromjson( "{'_id':0,a:[2,3]}" ) ); } }; - class PushTrimToZero : public SetBase { + class PushSliceToZero : public SetBase { public: void run() { client().insert( ns(), fromjson( "{'_id':0,a:[1,2]}" ) ); - // { $push : { a : { $each : [ 3 ] , $trimTo : 0 } } } - BSONObj pushObj = BSON( "$each" << BSON_ARRAY( 3 ) << "$trimTo" << 0 ); + // { $push : { a : { $each : [ 3 ] , $slice : 0 } } } + BSONObj pushObj = BSON( "$each" << BSON_ARRAY( 3 ) << "$slice" << 0 ); client().update( ns(), Query(), BSON( "$push" << BSON( "a" << pushObj ) ) ); ASSERT_EQUALS( client().findOne( ns(), Query() ) , fromjson( "{'_id':0,a:[]}" ) ); } }; - class PushTrimToZeroFromNothing : public SetBase { + class PushSliceToZeroFromNothing : public SetBase { public: void run() { client().insert( ns(), fromjson( "{'_id':0}" ) ); - // { $push : { a : { $each : [ 3 ] , $trimTo : 0 } } } - BSONObj pushObj = BSON( "$each" << BSON_ARRAY( 3 ) << "$trimTo" << 0 ); + // { $push : { a : { $each : [ 3 ] , $slice : 0 } } } + BSONObj pushObj = BSON( "$each" << BSON_ARRAY( 3 ) << "$slice" << 0 ); client().update( ns(), Query(), BSON( "$push" << BSON( "a" << pushObj ) ) ); ASSERT_EQUALS( client().findOne( ns(), Query() ) , fromjson( "{'_id':0,a:[]}" ) ); } }; - class PushTrimFromNothing : public SetBase { + class PushSliceFromNothing : public SetBase { public: void run() { client().insert( ns(), fromjson( "{'_id':0}" ) ); - // { $push : { a : { $each : [ 1 , 2 ] , $trimTo : 3 } } } - BSONObj pushObj = BSON( "$each" << BSON_ARRAY( 1 << 2 ) << "$trimTo" << 3 ); + // { $push : { a : { $each : [ 1 , 2 ] , $slice : -3 } } } + BSONObj pushObj = BSON( "$each" << BSON_ARRAY( 1 << 2 ) << "$slice" << -3 ); client().update( ns(), Query(), BSON( "$push" << BSON( "a" << pushObj ) ) ); ASSERT_EQUALS( client().findOne( ns(), Query() ) , fromjson( "{'_id':0,a:[1,2]}" ) ); } }; - class PushTrimLongerThanTrimFromNothing : public SetBase { + class PushSliceLongerThanSliceFromNothing : public SetBase { public: void run() { client().insert( ns(), fromjson( "{'_id':0}" ) ); - // { $push : { a : { $each : [ 1 , 2 , 3 ] , $trimTo : 2 } } } - BSONObj pushObj = BSON( "$each" << BSON_ARRAY( 1 << 2 << 3 ) << "$trimTo" << 2 ); + // { $push : { a : { $each : [ 1 , 2 , 3 ] , $slice : -2 } } } + BSONObj pushObj = BSON( "$each" << BSON_ARRAY( 1 << 2 << 3 ) << "$slice" << -2 ); client().update( ns(), Query(), BSON( "$push" << BSON( "a" << pushObj ) ) ); ASSERT_EQUALS( client().findOne( ns(), Query() ) , fromjson( "{'_id':0,a:[2,3]}" ) ); } }; - class PushTrimFromEmpty : public SetBase { + class PushSliceFromEmpty : public SetBase { public: void run() { client().insert( ns(), fromjson( "{'_id':0,a:[]}" ) ); - // { $push : { a : { $each : [ 1 ] , $trimTo : 3 } } } - BSONObj pushObj = BSON( "$each" << BSON_ARRAY( 1 ) << "$trimTo" << 3 ); + // { $push : { a : { $each : [ 1 ] , $slice : -3 } } } + BSONObj pushObj = BSON( "$each" << BSON_ARRAY( 1 ) << "$slice" << -3 ); client().update( ns(), Query(), BSON( "$push" << BSON( "a" << pushObj ) ) ); ASSERT_EQUALS( client().findOne( ns(), Query() ) , fromjson( "{'_id':0,a:[1]}" ) ); } }; - class PushTrimLongerThanTrimFromEmpty : public SetBase { + class PushSliceLongerThanSliceFromEmpty : public SetBase { public: void run() { client().insert( ns(), fromjson( "{'_id':0,a:[]}" ) ); - // { $push : { a : { $each : [ 1 , 2 , 3 ] , $trimTo : 2 } } } - BSONObj pushObj = BSON( "$each" << BSON_ARRAY( 1 << 2 << 3 ) << "$trimTo" << 2 ); + // { $push : { a : { $each : [ 1 , 2 , 3 ] , $slice : -2 } } } + BSONObj pushObj = BSON( "$each" << BSON_ARRAY( 1 << 2 << 3 ) << "$slice" << -2 ); client().update( ns(), Query(), BSON( "$push" << BSON( "a" << pushObj ) ) ); ASSERT_EQUALS( client().findOne( ns(), Query() ) , fromjson( "{'_id':0,a:[2,3]}" ) ); } }; - class PushTrimTwoFields : public SetBase { + class PushSliceTwoFields : public SetBase { public: void run() { client().insert( ns(), fromjson( "{'_id':0,a:[1,2],b:[3,4]}" ) ); - // { $push: { a: { $each: [ 5 ] , $trimTo : 2 }, { b: $each: [ 6 ] , $trimTo: 1 } } } - BSONObj objA = BSON( "$each" << BSON_ARRAY( 5 ) << "$trimTo" << 2 ); - BSONObj objB = BSON( "$each" << BSON_ARRAY( 6 ) << "$trimTo" << 1 ); + // { $push: { a: { $each: [ 5 ] , $slice : -2 }, { b: $each: [ 6 ] , $slice: -1 } } } + BSONObj objA = BSON( "$each" << BSON_ARRAY( 5 ) << "$slice" << -2 ); + BSONObj objB = BSON( "$each" << BSON_ARRAY( 6 ) << "$slice" << -1 ); client().update( ns(), Query(), BSON( "$push" << BSON( "a" << objA << "b" << objB ) ) ); ASSERT_EQUALS( client().findOne( ns(), Query() ) , fromjson("{'_id':0,a:[2,5],b:[6]}")); } }; - class PushTrimAndNormal : public SetBase { + class PushSliceAndNormal : public SetBase { public: void run() { client().insert( ns(), fromjson( "{'_id':0,a:[1,2],b:[3]}" ) ); - // { $push : { a : { $each : [ 5 ] , $trimTo : 2 } , { b : 4 } } - BSONObj objA = BSON( "$each" << BSON_ARRAY( 5 ) << "$trimTo" << 2 ); + // { $push : { a : { $each : [ 5 ] , $slice : -2 } , { b : 4 } } + BSONObj objA = BSON( "$each" << BSON_ARRAY( 5 ) << "$slice" << -2 ); client().update( ns(), Query(), BSON("$push" << BSON("a" << objA << "b" << 4))); ASSERT_EQUALS(client().findOne(ns(), Query()) , fromjson("{'_id':0,a:[2,5],b:[3,4]}")); } }; - class PushTrimTwoFieldsConflict : public SetBase { + class PushSliceTwoFieldsConflict : public SetBase { public: void run() { client().insert( ns(), fromjson( "{'_id':0,a:[1],b:[3]}" ) ); - // { $push: { a: { $each: [ 5 ] , $trimTo: 2 } , { a: $each: [ 6 ] , $trimTo: 1 } } } - BSONObj objA = BSON( "$each" << BSON_ARRAY( 5 ) << "$trimTo" << 2 ); - BSONObj other = BSON( "$each" << BSON_ARRAY( 6 ) << "$trimTo" << 1 ); + // { $push: { a: { $each: [ 5 ] , $slice: -2 } , { a: $each: [ 6 ] , $slice: -1 } } } + BSONObj objA = BSON( "$each" << BSON_ARRAY( 5 ) << "$slice" << -2 ); + BSONObj other = BSON( "$each" << BSON_ARRAY( 6 ) << "$slice" << -1 ); client().update( ns(), Query(), BSON( "$push" << BSON( "a" << objA << "a" << other ) ) ); ASSERT(client().findOne( ns(), Query()).woCompare(fromjson("{'_id':0,a:[1],b:[3]}"))==0); } }; - class PushTrimAndNormalConflict : public SetBase { + class PushSliceAndNormalConflict : public SetBase { public: void run() { client().insert( ns(), fromjson( "{'_id':0,a:[1],b:[3]}" ) ); - // { $push : { a : { $each : [ 5 ] , $trimTo : 2 } , { a : 4 } } } - BSONObj objA = BSON( "$each" << BSON_ARRAY( 5 ) << "$trimTo" << 2 ); + // { $push : { a : { $each : [ 5 ] , $slice : -2 } , { a : 4 } } } + BSONObj objA = BSON( "$each" << BSON_ARRAY( 5 ) << "$slice" << -2 ); client().update( ns(), Query(), BSON( "$push" << BSON( "a" << objA << "a" << 4 ) ) ); ASSERT(client().findOne( ns(), Query()).woCompare(fromjson("{'_id':0,a:[1],b:[3]}"))==0); } }; - class PushTrimInvalidEachType : public SetBase { + class PushSliceInvalidEachType : public SetBase { public: void run() { client().insert( ns(), fromjson( "{'_id':0,a:[1,2]}" ) ); - // { $push : { a : { $each : 3 , $trimTo : 2 } } } - BSONObj pushObj = BSON( "$each" << 3 << "$trimTo" << 2 ); + // { $push : { a : { $each : 3 , $slice : -2 } } } + BSONObj pushObj = BSON( "$each" << 3 << "$slice" << -2 ); client().update( ns(), Query(), BSON( "$push" << BSON( "a" << pushObj ) ) ); ASSERT( client().findOne(ns(), Query()).woCompare(fromjson("{'_id':0,a:[1,2]}")) == 0); } }; - class PushTrimInvalidTrimType : public SetBase { + class PushSliceInvalidSliceType : public SetBase { public: void run() { client().insert( ns(), fromjson( "{'_id':0,a:[1,2]}" ) ); - // { $push : { a : { $each : [ 3 ], $trimTo : [ 2 ] } } } - BSONObj pushObj = BSON( "$each" << BSON_ARRAY(3) << "$trimTo" << BSON_ARRAY(2) ); + // { $push : { a : { $each : [ 3 ], $slice : [ -2 ] } } } + BSONObj pushObj = BSON( "$each" << BSON_ARRAY(3) << "$slice" << BSON_ARRAY(-2) ); client().update( ns(), Query(), BSON( "$push" << BSON( "a" << pushObj ) ) ); ASSERT( client().findOne( ns(), Query() ).woCompare(fromjson("{'_id':0,a:[1,2]}")) == 0); } }; - class PushTrimInvalidTrimValue : public SetBase { + class PushSliceInvalidSliceValue : public SetBase { public: void run() { client().insert( ns(), fromjson( "{'_id':0,a:[1,2]}" ) ); - // { $push : { a : { $each : [ 3 ], $trimTo : - 2 } } } - BSONObj pushObj = BSON( "$each" << BSON_ARRAY(3) << "$trimTo" << -2 ); + // { $push : { a : { $each : [ 3 ], $slice : 2 } } } + BSONObj pushObj = BSON( "$each" << BSON_ARRAY(3) << "$slice" << 2 ); client().update( ns(), Query(), BSON( "$push" << BSON( "a" << pushObj ) ) ); ASSERT( client().findOne( ns(), Query() ).woCompare(fromjson("{'_id':0,a:[1,2]}")) == 0); } }; - class PushTrimInvalidTrimDouble : public SetBase { + class PushSliceInvalidSliceDouble : public SetBase { public: void run() { client().insert( ns(), fromjson( "{'_id':0,a:[1,2]}" ) ); - // { $push : { a : { $each : [ 3 ], $trimTo : 2.1 } } } - BSONObj pushObj = BSON( "$each" << BSON_ARRAY(3) << "$trimTo" << 2.1 ); + // { $push : { a : { $each : [ 3 ], $slice : -2.1 } } } + BSONObj pushObj = BSON( "$each" << BSON_ARRAY(3) << "$slice" << -2.1 ); client().update( ns(), Query(), BSON( "$push" << BSON( "a" << pushObj ) ) ); ASSERT( client().findOne( ns(), Query() ).woCompare(fromjson("{'_id':0,a:[1,2]}")) == 0); } }; - class PushTrimValidTrimDouble : public SetBase { + class PushSliceValidSliceDouble : public SetBase { public: void run() { client().insert( ns(), fromjson( "{'_id':0,a:[1,2]}" ) ); - // { $push : { a : { $each : [ 3 ], $trimTo : 2.0 } } } - BSONObj pushObj = BSON( "$each" << BSON_ARRAY(3) << "$trimTo" << 2.0 ); + // { $push : { a : { $each : [ 3 ], $slice : -2.0 } } } + BSONObj pushObj = BSON( "$each" << BSON_ARRAY(3) << "$slice" << -2.0 ); client().update( ns(), Query(), BSON( "$push" << BSON( "a" << pushObj ) ) ); ASSERT_EQUALS(client().findOne(ns(), Query()) , fromjson("{'_id':0,a:[2,3]}")); } }; - class PushTrimInvalidTrim : public SetBase { + class PushSliceInvalidSlice : public SetBase { public: void run() { client().insert( ns(), fromjson( "{'_id':0,a:[1,2]}" ) ); @@ -685,7 +685,7 @@ namespace UpdateTests { }; // - // We'd like to test the hability of $push with $sort in the following sequence of test. We + // We'd like to test the ability of $push with $sort in the following sequence of tests. We // try to enumerate all the possibilities of where the final element would come from: the // document, the $push itself, or both. // @@ -697,7 +697,19 @@ namespace UpdateTests { } protected: - enum { TOPK_ASC = 0, TOPK_DESC = 1, BOTTOMK_ASC = 2, BOTTOMK_DESC = 3 }; + enum UpdateType { + // Sorts ascending and slices the back of the array. + TOPK_ASC = 0, + + // Sorts descending and slices the front of the array. + TOPK_DESC = 1, + + // Sorts ascending and slices the back of the array. + BOTTOMK_ASC = 2, + + // Sorts descending and slices the front of the array. + BOTTOMK_DESC = 3 + }; const char* ns() { return "unittest.updatetests.PushSortBase"; @@ -710,10 +722,14 @@ namespace UpdateTests { _fieldNames = fields; _fieldValues = values; _sortFields = sort; - _trimSize = size; + _sliceSize = size; } - BSONObj getUpdate( int i ) { + /** + * Generates the update expression portion of an update command given one of the + * possible types of update. + */ + BSONObj getUpdate( int updateType ) { BSONObjBuilder updateBuilder; BSONObjBuilder pushBuilder( updateBuilder.subobjStart( "$push" ) ); BSONObjBuilder fieldBuilder( pushBuilder.subobjStart( "x" ) ); @@ -733,15 +749,17 @@ namespace UpdateTests { } eachBuilder.done(); - // Builds $trimTo portion. - fieldBuilder.append( "$trimTo", _trimSize ); + // Builds $slice portion. + fieldBuilder.append( "$slice", + updateType < 2 ? -_sliceSize : _sliceSize); // Builds $sort: <sort pattern> portion BSONObjBuilder patternBuilder( fieldBuilder.subobjStart( "$sort" ) ); BSONObjIterator itSort( _sortFields ); while ( itSort.more() ) { BSONElement sortField = itSort.next(); - patternBuilder.append( sortField.String(), i%2 ? -1 : 1 ); + patternBuilder.append( sortField.String(), + updateType%2 ? -1 : 1 ); } patternBuilder.done(); @@ -761,7 +779,7 @@ namespace UpdateTests { BSONArray _fieldNames; BSONArray _fieldValues; BSONArray _sortFields; - int _trimSize; + int _sliceSize; }; class PushSortBelowFull : public PushSortBase { @@ -772,13 +790,13 @@ namespace UpdateTests { // the each array each array field to sort size setParams( BSON_ARRAY( "a" << "b" ), BSON_ARRAY( 2 ), BSON_ARRAY( "b" ), 3 ); - // Generates the four variations below - // TOPK_ASC: $push: { x: { $each: [ {a:2,b:2} ], $trimTo:3, $sort: { b:1 } } } - // TOPK_DESC: $push: { x: { $each: [ {a:2,b:2} ], $trimTo:3, $sort: { b:-1 } } } - // BOTTOMK_ASC: $push: { x: { $each: [ {a:2,b:2} ], $trimTo:-3, $sort: { b:1 } } } - // BOTTOMK_DESC: $push: { x: { $each: [ {a:2,b:2} ], $trimTo:-3, $sort: { b:-1 } } } + // Generates the four variations below (but for now we're only using negative slice). + // TOPK_ASC: $push: { x: { $each: [ {a:2,b:2} ], $slice:-3, $sort: { b:1 } } } + // TOPK_DESC: $push: { x: { $each: [ {a:2,b:2} ], $slice:-3, $sort: { b:-1 } } } + // BOTTOMK_ASC: $push: { x: { $each: [ {a:2,b:2} ], $slice:3, $sort: { b:1 } } } + // BOTTOMK_DESC: $push: { x: { $each: [ {a:2,b:2} ], $slice:3, $sort: { b:-1 } } } - for ( int i = 0; i < 2; i++ ) { // i < 4 when we have negative $trimTo + for ( int i = 0; i < 2; i++ ) { // i < 4 when we have positive $slice client().dropCollection( ns() ); client().insert( ns(), fromjson( "{'_id':0,x:[{a:1,b:1}]}" ) ); @@ -789,7 +807,7 @@ namespace UpdateTests { case BOTTOMK_ASC: client().update( ns(), Query(), getUpdate(i) ); result = client().findOne( ns(), Query() ); - expected = fromjson( "{'_id':0,x:[{a:1,b:1},{a:2,b:2}]}" ); + expected = fromjson( "{'_id':0,x:[{a:1,b:1},{a:2,b:2}]}" ); ASSERT_EQUALS( result, expected ); break; @@ -813,13 +831,13 @@ namespace UpdateTests { // the each array each array field to sort size setParams(BSON_ARRAY( "a"<<"b" ), BSON_ARRAY( 2 ), BSON_ARRAY( "b" ), 2 ); - // Generates the four variations below - // TOPK_ASC: $push: { x: { $each: [ {a:2,b:2} ], $trimTo:2, $sort: { b:1 } } } - // TOPK_DESC: $push: { x: { $each: [ {a:2,b:2} ], $trimTo:2, $sort: { b:-1 } } } - // BOTTOMK_ASC: $push: { x: { $each: [ {a:2,b:2} ], $trimTo:-2, $sort: { b:1 } } } - // BOTTOMK_DESC: $push: { x: { $each: [ {a:2,b:2} ], $trimTo:-2, $sort: { b:-1 } } } + // Generates the four variations below (but for now we're only using negative slice). + // TOPK_ASC: $push: { x: { $each: [ {a:2,b:2} ], $slice:-2, $sort: { b:1 } } } + // TOPK_DESC: $push: { x: { $each: [ {a:2,b:2} ], $slice:-2, $sort: { b:-1 } } } + // BOTTOMK_ASC: $push: { x: { $each: [ {a:2,b:2} ], $slice:2, $sort: { b:1 } } } + // BOTTOMK_DESC: $push: { x: { $each: [ {a:2,b:2} ], $slice:2, $sort: { b:-1 } } } - for ( int i = 0; i < 2; i++ ) { // i < 4 when we have negative $trimTo + for ( int i = 0; i < 2; i++ ) { // i < 4 when we have positive $slice client().dropCollection( ns() ); client().insert( ns(), fromjson( "{'_id':0,x:[{a:1,b:1}]}" ) ); @@ -854,13 +872,13 @@ namespace UpdateTests { // the each array each array field to sort size setParams( BSON_ARRAY( "a"<<"b" ), BSON_ARRAY( 2 ), BSON_ARRAY( "b" ), 2 ); - // Generates the four variations below - // TOPK_ASC: $push: { x: { $each: [ {a:2,b:2} ], $trimTo:2, $sort: { b:1 } } } - // TOPK_DESC: $push: { x: { $each: [ {a:2,b:2} ], $trimTo:2, $sort: { b:-1 } } } - // BOTTOMK_ASC: $push: { x: { $each: [ {a:2,b:2} ], $trimTo:-2, $sort: { b:1 } } } - // BOTTOMK_DESC: $push: { x: { $each: [ {a:2,b:2} ], $trimTo:-2, $sort: { b:-1 } } } + // Generates the four variations below (but for now we're only using negative slice). + // TOPK_ASC: $push: { x: { $each: [ {a:2,b:2} ], $slice:-2, $sort: { b:1 } } } + // TOPK_DESC: $push: { x: { $each: [ {a:2,b:2} ], $slice:-2, $sort: { b:-1 } } } + // BOTTOMK_ASC: $push: { x: { $each: [ {a:2,b:2} ], $slice:2, $sort: { b:1 } } } + // BOTTOMK_DESC: $push: { x: { $each: [ {a:2,b:2} ], $slice:2, $sort: { b:-1 } } } - for ( int i = 0; i < 2; i++ ) { // i < 4 when we have negative $trimTo + for ( int i = 0; i < 2; i++ ) { // i < 4 when we have positive $slice client().dropCollection( ns() ); client().insert( ns(), fromjson( "{'_id':0,x:[{a:1,b:1},{a:3,b:3}]}" ) ); @@ -898,13 +916,13 @@ namespace UpdateTests { // the each array each array field to sort size setParams( BSON_ARRAY( "a"<<"b" ), BSON_ARRAY( 2 ), BSON_ARRAY( "b" ), 0 ); - // Generates the four variations below - // TOPK_ASC: $push: { x: { $each: [ {a:2,b:2} ], $trimTo:0, $sort: { b:1 } } } - // TOPK_DESC: $push: { x: { $each: [ {a:2,b:2} ], $trimTo:0, $sort: { b:-1 } } } - // BOTTOMK_ASC: $push: { x: { $each: [ {a:2,b:2} ], $trimTo:0, $sort: { b:1 } } } - // BOTTOMK_DESC: $push: { x: { $each: [ {a:2,b:2} ], $trimTo:0, $sort: { b:-1 } } } + // Generates the four variations below (but for now we're only using negative slice). + // TOPK_ASC: $push: { x: { $each: [ {a:2,b:2} ], $slice:0, $sort: { b:1 } } } + // TOPK_DESC: $push: { x: { $each: [ {a:2,b:2} ], $slice:0, $sort: { b:-1 } } } + // BOTTOMK_ASC: $push: { x: { $each: [ {a:2,b:2} ], $slice:0, $sort: { b:1 } } } + // BOTTOMK_DESC: $push: { x: { $each: [ {a:2,b:2} ], $slice:0, $sort: { b:-1 } } } - for ( int i = 0; i < 2; i++ ) { // i < 4 when we have negative $trimTo + for ( int i = 0; i < 2; i++ ) { // i < 4 when we have positive $slice client().dropCollection( ns() ); client().insert( ns(), fromjson( "{'_id':0,x:[{a:1,b:1},{a:3,b:3}]}" ) ); @@ -930,13 +948,13 @@ namespace UpdateTests { // the each array each array field to sort size setParams( BSON_ARRAY( "a"<<"b" ), BSON_ARRAY( 2 ), BSON_ARRAY( "b" ), 0 ); - // Generates the four variations below - // TOPK_ASC: $push: { x: { $each: [ {a:2,b:2} ], $trimTo:0, $sort: { b:1 } } } - // TOPK_DESC: $push: { x: { $each: [ {a:2,b:2} ], $trimTo:0, $sort: { b:-1 } } } - // BOTTOMK_ASC: $push: { x: { $each: [ {a:2,b:2} ], $trimTo:0, $sort: { b:1 } } } - // BOTTOMK_DESC: $push: { x: { $each: [ {a:2,b:2} ], $trimTo:0, $sort: { b:-1 } } } + // Generates the four variations below (but for now we're only using negative slice). + // TOPK_ASC: $push: { x: { $each: [ {a:2,b:2} ], $slice:0, $sort: { b:1 } } } + // TOPK_DESC: $push: { x: { $each: [ {a:2,b:2} ], $slice:0, $sort: { b:-1 } } } + // BOTTOMK_ASC: $push: { x: { $each: [ {a:2,b:2} ], $slice:0, $sort: { b:1 } } } + // BOTTOMK_DESC: $push: { x: { $each: [ {a:2,b:2} ], $slice:0, $sort: { b:-1 } } } - for ( int i = 0; i < 2; i++ ) { // i < 4 when we have negative $trimTo + for ( int i = 0; i < 2; i++ ) { // i < 4 when we have positive $slice client().dropCollection( ns() ); client().insert( ns(), fromjson( "{'_id':0}" ) ); @@ -962,15 +980,15 @@ namespace UpdateTests { // the each array each array field to sort size setParams(BSON_ARRAY( "a"<<"b" ), BSON_ARRAY( 2 << 1 ), BSON_ARRAY( "b" ), 2 ); - // Generates the four variations below + // Generates the four variations below (but for now we're only using negative slice). // <genarr> = [ {a:2,b:2}, {a:1,b:1} ] // Generates the four variations below - // TOPK_ASC: $push: { x: { $each: [ <genarray> ], $trimTo:2, $sort: { b:1 } } } - // TOPK_DESC: $push: { x: { $each: [ <genarray> ], $trimTo:2, $sort: { b:-1 } } } - // BOTTOMK_ASC: $push: { x: { $each: [ <genarray> ], $trimTo:-2, $sort: { b:1 } } } - // BOTTOMK_DESC: $push: { x: { $each: [ <genarray> ], $trimTo:-2, $sort: { b:-1 } } } + // TOPK_ASC: $push: { x: { $each: [ <genarray> ], $slice:-2, $sort: { b:1 } } } + // TOPK_DESC: $push: { x: { $each: [ <genarray> ], $slice:-2, $sort: { b:-1 } } } + // BOTTOMK_ASC: $push: { x: { $each: [ <genarray> ], $slice:2, $sort: { b:1 } } } + // BOTTOMK_DESC: $push: { x: { $each: [ <genarray> ], $slice:2, $sort: { b:-1 } } } - for ( int i = 0; i < 2; i++ ) { // i < 4 when we have negative $trimTo + for ( int i = 0; i < 2; i++ ) { // i < 4 when we have positive $slice client().dropCollection( ns() ); client().insert( ns(), fromjson( "{'_id':0}" ) ); @@ -997,7 +1015,7 @@ namespace UpdateTests { } }; - class PushSortLongerThanTrimFromNothing : public PushSortBase { + class PushSortLongerThanSliceFromNothing : public PushSortBase { public: void run() { // With the following parameters @@ -1005,14 +1023,14 @@ namespace UpdateTests { // the each array each array field to sort size setParams(BSON_ARRAY( "a"<<"b" ), BSON_ARRAY( 2 << 1 << 3), BSON_ARRAY( "b" ), 2 ); - // Generates the four variations below + // Generates the four variations below (but for now we're only using negative slice). // <genarr> = [ {a:2,b:2}, {a:1,b:1}, {a:3,b:3} ] - // TOPK_ASC: $push: { x: { $each: [ <genarray> ], $trimTo:2, $sort: { b:1 } } } - // TOPK_DESC: $push: { x: { $each: [ <genarray> ], $trimTo:2, $sort: { b:-1 } } } - // BOTTOMK_ASC: $push: { x: { $each: [ <genarray> ], $trimTo:-2, $sort: { b:1 } } } - // BOTTOMK_DESC: $push: { x: { $each: [ <genarray> ], $trimTo:-2, $sort: { b:-1 } } } + // TOPK_ASC: $push: { x: { $each: [ <genarray> ], $slice:-2, $sort: { b:1 } } } + // TOPK_DESC: $push: { x: { $each: [ <genarray> ], $slice:-2, $sort: { b:-1 } } } + // BOTTOMK_ASC: $push: { x: { $each: [ <genarray> ], $slice:2, $sort: { b:1 } } } + // BOTTOMK_DESC: $push: { x: { $each: [ <genarray> ], $slice:2, $sort: { b:-1 } } } - for ( int i = 0; i < 2; i++ ) { // i < 4 when we have negative $trimTo + for ( int i = 0; i < 2; i++ ) { // i < 4 when we have positive $slice client().dropCollection( ns() ); client().insert( ns(), fromjson( "{'_id':0}" ) ); @@ -1050,14 +1068,14 @@ namespace UpdateTests { // the each array each array field to sort size setParams(BSON_ARRAY( "a"<<"b" ), BSON_ARRAY( 2 << 1 ), BSON_ARRAY( "b" ), 2 ); - // Generates the four variations below + // Generates the four variations below (but for now we're only using negative slice). // <genarr> = [ {a:2,b:2}, {a:1,b:1} ] - // TOPK_ASC: $push: { x: { $each: [ <genarray> ], $trimTo:2, $sort: { b:1 } } } - // TOPK_DESC: $push: { x: { $each: [ <genarray> ], $trimTo:2, $sort: { b:-1 } } } - // BOTTOMK_ASC: $push: { x: { $each: [ <genarray> ], $trimTo:-2, $sort: { b:1 } } } - // BOTTOMK_DESC: $push: { x: { $each: [ <genarray> ], $trimTo:-2, $sort: { b:-1 } } } + // TOPK_ASC: $push: { x: { $each: [ <genarray> ], $slice:-2, $sort: { b:1 } } } + // TOPK_DESC: $push: { x: { $each: [ <genarray> ], $slice:-2, $sort: { b:-1 } } } + // BOTTOMK_ASC: $push: { x: { $each: [ <genarray> ], $slice:2, $sort: { b:1 } } } + // BOTTOMK_DESC: $push: { x: { $each: [ <genarray> ], $slice:2, $sort: { b:-1 } } } - for ( int i = 0; i < 2; i++ ) { // i < 4 when we have negative $trimTo + for ( int i = 0; i < 2; i++ ) { // i < 4 when we have positive $slice client().dropCollection( ns() ); client().insert( ns(), fromjson( "{'_id':0,x:[]}" ) ); @@ -1084,7 +1102,7 @@ namespace UpdateTests { } }; - class PushSortLongerThanTrimFromEmpty : public PushSortBase { + class PushSortLongerThanSliceFromEmpty : public PushSortBase { public: void run() { // With the following parameters @@ -1092,14 +1110,14 @@ namespace UpdateTests { // the each array each array field to sort size setParams(BSON_ARRAY( "a"<<"b" ), BSON_ARRAY( 2 << 1 << 3), BSON_ARRAY( "b" ), 2 ); - // Generates the four variations below + // Generates the four variations below (but for now we're only using negative slice). // <genarr> = [ {a:2,b:2}, {a:1,b:1}, {a:3,b:3} ] - // TOPK_ASC: $push: { x: { $each: [ <genarray> ], $trimTo:2, $sort: { b:1 } } } - // TOPK_DESC: $push: { x: { $each: [ <genarray> ], $trimTo:2, $sort: { b:-1 } } } - // BOTTOMK_ASC: $push: { x: { $each: [ <genarray> ], $trimTo:-2, $sort: { b:1 } } } - // BOTTOMK_DESC: $push: { x: { $each: [ <genarray> ], $trimTo:-2, $sort: { b:-1 } } } + // TOPK_ASC: $push: { x: { $each: [ <genarray> ], $slice:-2, $sort: { b:1 } } } + // TOPK_DESC: $push: { x: { $each: [ <genarray> ], $slice:-2, $sort: { b:-1 } } } + // BOTTOMK_ASC: $push: { x: { $each: [ <genarray> ], $slice:2, $sort: { b:1 } } } + // BOTTOMK_DESC: $push: { x: { $each: [ <genarray> ], $slice:2, $sort: { b:-1 } } } - for ( int i = 0; i < 2; i++ ) { // i < 4 when we have negative $trimTo + for ( int i = 0; i < 2; i++ ) { // i < 4 when we have positive $slice client().dropCollection( ns() ); client().insert( ns(), fromjson( "{'_id':0,x:[]}" ) ); @@ -1133,9 +1151,9 @@ namespace UpdateTests { public: void run() { BSONObj objs[3]; - objs[0] = fromjson ( "{a:1, b:1}" ); - objs[1] = fromjson ( "{a:3, b:1}" ); - objs[2] = fromjson ( "{a:2, b:3}" ); + objs[0] = fromjson( "{a:1, b:1}" ); + objs[1] = fromjson( "{a:3, b:1}" ); + objs[2] = fromjson( "{a:2, b:3}" ); vector<BSONObj> workArea; for ( int i = 0; i < 3; i++ ) { @@ -1154,9 +1172,9 @@ namespace UpdateTests { public: void run() { BSONObj objs[3]; - objs[0] = fromjson ( "{b:1, a:1}" ); - objs[1] = fromjson ( "{a:3, b:2}" ); - objs[2] = fromjson ( "{b:3, a:2}" ); + objs[0] = fromjson( "{b:1, a:1}" ); + objs[1] = fromjson( "{a:3, b:2}" ); + objs[2] = fromjson( "{b:3, a:2}" ); vector<BSONObj> workArea; for ( int i = 0; i < 3; i++ ) { @@ -1175,9 +1193,9 @@ namespace UpdateTests { public: void run() { BSONObj objs[3]; - objs[0] = fromjson ( "{b:1, c:2, a:1}" ); - objs[1] = fromjson ( "{c:1, a:3, b:2}" ); - objs[2] = fromjson ( "{b:3, a:2}" ); + objs[0] = fromjson( "{b:1, c:2, a:1}" ); + objs[1] = fromjson( "{c:1, a:3, b:2}" ); + objs[2] = fromjson( "{b:3, a:2}" ); vector<BSONObj> workArea; for ( int i = 0; i < 3; i++ ) { @@ -1196,9 +1214,9 @@ namespace UpdateTests { public: void run() { BSONObj objs[3]; - objs[0] = fromjson ( "{a:2, b:2}" ); - objs[1] = fromjson ( "{a:1}" ); - objs[2] = fromjson ( "{a:3, b:3, c:3}" ); + objs[0] = fromjson( "{a:2, b:2}" ); + objs[1] = fromjson( "{a:1}" ); + objs[2] = fromjson( "{a:3, b:3, c:3}" ); vector<BSONObj> workArea; for ( int i = 0; i < 3; i++ ) { @@ -1213,14 +1231,74 @@ namespace UpdateTests { } }; + class PushSortSortNestedFields { + public: + void run() { + BSONObj objs[3]; + objs[0] = fromjson( "{a:{b:{c:2, d:0}}}" ); + objs[1] = fromjson( "{a:{b:{c:1, d:2}}}" ); + objs[2] = fromjson( "{a:{b:{c:3, d:1}}}" ); + + vector<BSONObj> workArea; + for ( int i = 0; i < 3; i++ ) { + workArea.push_back( objs[i] ); + } + + sort( workArea.begin(), workArea.end(), ProjectKeyCmp( fromjson( "{'a.b.d':-1}" ) ) ); + + ASSERT_EQUALS( workArea[0], objs[1] ); + ASSERT_EQUALS( workArea[1], objs[2] ); + ASSERT_EQUALS( workArea[2], objs[0] ); + + sort( workArea.begin(), workArea.end(), ProjectKeyCmp( fromjson( "{'a.b':1}" ) ) ); + + ASSERT_EQUALS( workArea[0], objs[1] ); + ASSERT_EQUALS( workArea[1], objs[0] ); + ASSERT_EQUALS( workArea[2], objs[2] ); + + } + }; + + class PushSortInvalidSortPattern { + public: + void run() { + vector<BSONObj> dummy; + + ASSERT_THROWS( sort( dummy.begin(), + dummy.end(), + ProjectKeyCmp( fromjson( "{'a..d':-1}" ) ) ), + UserException ); + + ASSERT_THROWS( sort( dummy.begin(), + dummy.end(), + ProjectKeyCmp( fromjson( "{'a.':-1}" ) ) ), + UserException ); + + ASSERT_THROWS( sort( dummy.begin(), + dummy.end(), + ProjectKeyCmp( fromjson( "{'.b':-1}" ) ) ), + UserException ); + + ASSERT_THROWS( sort( dummy.begin(), + dummy.end(), + ProjectKeyCmp( fromjson( "{'.':-1}" ) ) ), + UserException ); + + ASSERT_THROWS( sort( dummy.begin(), + dummy.end(), + ProjectKeyCmp( fromjson( "{'':-1}" ) ) ), + UserException ); + } + }; + class PushSortInvalidEachType : public SetBase { public: void run() { BSONObj expected = fromjson( "{'_id':0,x:[{a:1},{a:2}]}" ); client().insert( ns(), expected ); - // { $push : { x : { $each : [ 3 ], $trimTo:2, $sort : {a:1} } } } + // { $push : { x : { $each : [ 3 ], $slice:-2, $sort : {a:1} } } } BSONObj pushObj = BSON( "$each" << BSON_ARRAY( 3 ) << - "$trimTo" << 2 << + "$slice" << -2 << "$sort" << BSON( "a" << 1 ) ); client().update( ns(), Query(), BSON( "$push" << BSON( "x" << pushObj ) ) ); BSONObj result = client().findOne( ns(), Query() ); @@ -1233,9 +1311,9 @@ namespace UpdateTests { void run() { BSONObj expected = fromjson( "{'_id':0,x:[1,2]}" ); client().insert( ns(), expected ); - // { $push : { x : { $each : [ {a:3} ], $trimTo:2, $sort : {a:1} } } } + // { $push : { x : { $each : [ {a:3} ], $slice:-2, $sort : {a:1} } } } BSONObj pushObj = BSON( "$each" << BSON_ARRAY( BSON( "a" << 3 ) ) << - "$trimTo" << 2 << + "$slice" << -2 << "$sort" << BSON( "a" << 1 ) ); client().update( ns(), Query(), BSON( "$push" << BSON( "x" << pushObj ) ) ); BSONObj result = client().findOne( ns(), Query() ); @@ -1248,9 +1326,9 @@ namespace UpdateTests { void run() { BSONObj expected = fromjson( "{'_id':0,x:[{a:1},{a:2}]}" ); client().insert( ns(), expected ); - // { $push : { x : { $each : [ {a:3} ], $trimTo:2, $sort : 2} } } + // { $push : { x : { $each : [ {a:3} ], $slice:-2, $sort : 2} } } BSONObj pushObj = BSON( "$each" << BSON_ARRAY( BSON( "a" << 3 ) ) << - "$trimTo" << 2 << + "$slice" << -2 << "$sort" << 2 ); client().update( ns(), Query(), BSON( "$push" << BSON( "x" << pushObj ) ) ); BSONObj result = client().findOne( ns(), Query() ); @@ -1263,9 +1341,9 @@ namespace UpdateTests { void run() { BSONObj expected = fromjson( "{'_id':0,x:[{a:1},{a:2}]}" ); client().insert( ns(), expected ); - // { $push : { x : { $each : [ {a:3} ], $trimTo: -2, $sort : {a:1} } } } + // { $push : { x : { $each : [ {a:3} ], $slice:2, $sort : {a:1} } } } BSONObj pushObj = BSON( "$each" << BSON_ARRAY( BSON( "a" << 3 ) ) << - "$trimTo" << -2 << + "$slice" << 2 << "$sort" << BSON( "a" << 1 ) ); client().update( ns(), Query(), BSON( "$push" << BSON( "x" << pushObj ) ) ); BSONObj result = client().findOne( ns(), Query() ); @@ -1278,9 +1356,9 @@ namespace UpdateTests { void run() { BSONObj expected = fromjson( "{'_id':0,x:[{a:1},{a:2}]}" ); client().insert( ns(), expected ); - // { $push : { x : { $each : [ {a:3} ], $trimTo: 2.1, $sort : {a:1} } } } + // { $push : { x : { $each : [ {a:3} ], $slice:-2.1, $sort : {a:1} } } } BSONObj pushObj = BSON( "$each" << BSON_ARRAY( BSON( "a" << 3 ) ) << - "$trimTo" << 2.1 << + "$slice" << -2.1 << "$sort" << BSON( "a" << 1 ) ); client().update( ns(), Query(), BSON( "$push" << BSON( "x" << pushObj ) ) ); BSONObj result = client().findOne( ns(), Query() ); @@ -1292,9 +1370,9 @@ namespace UpdateTests { public: void run() { client().insert( ns(), fromjson( "{'_id':0,x:[{a:1},{a:2}]}" ) ); - // { $push : { x : { $each : [ {a:3} ], $trimTo: 2.0, $sort : {a:1} } } } + // { $push : { x : { $each : [ {a:3} ], $slice:-2.0, $sort : {a:1} } } } BSONObj pushObj = BSON( "$each" << BSON_ARRAY( BSON( "a" << 3 ) ) << - "$trimTo" << 2.0 << + "$slice" << -2.0 << "$sort" << BSON( "a" << 1 ) ); client().update( ns(), Query(), BSON( "$push" << BSON( "x" << pushObj ) ) ); BSONObj expected = fromjson( "{'_id':0,x:[{a:2},{a:3}]}" ); @@ -1308,9 +1386,9 @@ namespace UpdateTests { void run() { BSONObj expected = fromjson( "{'_id':0,x:[{a:1},{a:2}]}" ); client().insert( ns(), expected ); - // { $push : { x : { $each : [ {a:3} ], $trimTo: 2.0, $sort : [2, 1] } } } + // { $push : { x : { $each : [ {a:3} ], $slice:-2.0, $sort : [2, 1] } } } BSONObj pushObj = BSON( "$each" << BSON_ARRAY( BSON( "a" << 3 ) ) << - "$trimTo" << 2.0 << + "$slice" << -2.0 << "$sort" << BSON_ARRAY( 2 << 1 ) ); client().update( ns(), Query(), BSON( "$push" << BSON( "x" << pushObj ) ) ); BSONObj result = client().findOne( ns(), Query() ); @@ -1323,9 +1401,9 @@ namespace UpdateTests { void run() { BSONObj expected = fromjson( "{'_id':0,x:[{a:1},{a:2}]}" ); client().insert( ns(), expected ); - // { $push : { x : { $each : [ {a:3} ], $trimTo:2, $sort : {a:10} } } } + // { $push : { x : { $each : [ {a:3} ], $slice:-2, $sort : {a:10} } } } BSONObj pushObj = BSON( "$each" << BSON_ARRAY( BSON( "a" << 3 ) ) << - "$trimTo" << 2 << + "$slice" << -2 << "$sort" << BSON( "a" << 10 ) ); client().update( ns(), Query(), BSON( "$push" << BSON( "x" << pushObj ) ) ); BSONObj result = client().findOne( ns(), Query() ); @@ -1333,14 +1411,14 @@ namespace UpdateTests { } }; - class PushSortInvertedSortAndTrim : public SetBase { + class PushSortInvertedSortAndSlice : public SetBase { public: void run() { client().insert( ns(), fromjson( "{'_id':0,x:[{a:1},{a:3}]}" ) ); - // { $push : { x : { $each : [ {a:2} ], $sort: {a:1}, $trimTo: 2 } } } + // { $push : { x : { $each : [ {a:2} ], $sort: {a:1}, $slice:-2 } } } BSONObj pushObj = BSON( "$each" << BSON_ARRAY( BSON( "a" << 2 ) ) << "$sort" << BSON( "a" << 1 ) << - "$trimTo" << 2.0 ); + "$slice" << -2.0 ); client().update( ns(), Query(), BSON( "$push" << BSON( "x" << pushObj ) ) ); BSONObj expected = fromjson( "{'_id':0,x:[{a:2},{a:3}]}" ); BSONObj result = client().findOne( ns(), Query() ); @@ -1365,7 +1443,7 @@ namespace UpdateTests { } }; - class PushSortInvalidMissingTrimTo : public SetBase { + class PushSortInvalidMissingSliceTo : public SetBase { public: void run() { BSONObj expected = fromjson( "{'_id':0,x:[{a:1},{a:3}]}" ); @@ -1704,12 +1782,12 @@ namespace UpdateTests { } }; - class PushTrimRewriteExistingField { + class PushSliceRewriteExistingField { public: void run() { BSONObj obj = BSON( "a" << BSON_ARRAY( 1 << 2 ) ); - // { $push : { a : { $each : [ 3 ] , $trimTo : 2 } } } - BSONObj pushObj = BSON( "$each" << BSON_ARRAY( 3 ) << "$trimTo" << 2 ); + // { $push : { a : { $each : [ 3 ] , $slice :-2 } } } + BSONObj pushObj = BSON( "$each" << BSON_ARRAY( 3 ) << "$slice" << -2 ); BSONObj mod = BSON( "$push" << BSON( "a" << pushObj ) ); ModSet modSet( mod ); auto_ptr<ModSetState> modSetState = modSet.prepare( obj ); @@ -1725,9 +1803,9 @@ namespace UpdateTests { void run() { BSONObj obj = BSON( "x" << BSON_ARRAY( BSON( "a" << 1 ) << BSON( "a" << 2 ) ) ); - // { $push : { a : { $each : [ {a:3} ], $trimTo:2, $sort : {a:1} } } } + // { $push : { a : { $each : [ {a:3} ], $slice:-2, $sort : {a:1} } } } BSONObj pushObj = BSON( "$each" << BSON_ARRAY( BSON( "a" << 3 ) ) << - "$trimTo" << 2 << + "$slice" << -2 << "$sort" << BSON( "a" << 1 ) ); BSONObj mod = BSON( "$push" << BSON( "x" << pushObj ) ); ModSet modSet( mod ); @@ -1755,12 +1833,12 @@ namespace UpdateTests { } }; - class PushTrimRewriteNonExistingField { + class PushSliceRewriteNonExistingField { public: void run() { BSONObj obj = BSON( "b" << 1 ); - // { $push : { a : { $each : [ 1 , 2 ] , $trimTo : 2 } } } - BSONObj pushObj = BSON( "$each" << BSON_ARRAY( 1 << 2) << "$trimTo" << 2 ); + // { $push : { a : { $each : [ 1 , 2 ] , $slice:-2 } } } + BSONObj pushObj = BSON( "$each" << BSON_ARRAY( 1 << 2) << "$slice" << -2 ); BSONObj mod = BSON( "$push" << BSON( "a" << pushObj ) ); ModSet modSet( mod ); auto_ptr<ModSetState> modSetState = modSet.prepare( obj ); @@ -1771,11 +1849,11 @@ namespace UpdateTests { } }; - class PushTrimRewriteNested { + class PushSliceRewriteNested { public: void run() { BSONObj obj = fromjson( "{ a:{ b:[ 1, 2 ] } }" ); - BSONObj mod = fromjson( "{ $push: { 'a.b': { $each: [3] , $trimTo: 2 } } }" ); + BSONObj mod = fromjson( "{ $push: { 'a.b': { $each: [3] , $slice:-2 } } }" ); ModSet modSet( mod ); auto_ptr<ModSetState> modSetState = modSet.prepare( obj ); ASSERT_FALSE( modSetState->canApplyInPlace() ); @@ -1789,10 +1867,10 @@ namespace UpdateTests { public: void run() { BSONObj obj = BSON( "b" << 1 ); - // { $push : { x : { $each : [ {a:1},{a:2} ], $trimTo:2, $sort : {a:1} } } } + // { $push : { x : { $each : [ {a:1},{a:2} ], $slice:-2, $sort : {a:1} } } } BSONObj pushObj = BSON( "$each" << BSON_ARRAY( BSON( "a" << 1 ) << BSON( "a" << 2 ) ) << - "$trimTo" << 2 << + "$slice" << -2 << "$sort" << BSON( "a" << 1 ) ); BSONObj mod = BSON( "$push" << BSON( "x" << pushObj ) ); ModSet modSet( mod ); @@ -2478,39 +2556,41 @@ namespace UpdateTests { add< CantPushTwice >(); add< SetEncapsulationConflictsWithExistingType >(); add< CantPushToParent >(); - add< PushTrimBelowFull >(); - add< PushTrimReachedFullExact >(); - add< PushTrimReachedFullWithEach >(); - add< PushTrimReachedFullWithBoth >(); - add< PushTrimToZero >(); - add< PushTrimToZeroFromNothing >(); - add< PushTrimFromNothing >(); - add< PushTrimLongerThanTrimFromNothing >(); - add< PushTrimFromEmpty >(); - add< PushTrimLongerThanTrimFromEmpty >(); - add< PushTrimTwoFields >(); - add< PushTrimAndNormal >(); - add< PushTrimTwoFieldsConflict >(); - add< PushTrimAndNormalConflict >(); - add< PushTrimInvalidEachType >(); - add< PushTrimInvalidTrimType >(); - add< PushTrimInvalidTrimValue >(); - add< PushTrimInvalidTrimDouble >(); - add< PushTrimValidTrimDouble >(); - add< PushTrimInvalidTrim >(); + add< PushSliceBelowFull >(); + add< PushSliceReachedFullExact >(); + add< PushSliceReachedFullWithEach >(); + add< PushSliceReachedFullWithBoth >(); + add< PushSliceToZero >(); + add< PushSliceToZeroFromNothing >(); + add< PushSliceFromNothing >(); + add< PushSliceLongerThanSliceFromNothing >(); + add< PushSliceFromEmpty >(); + add< PushSliceLongerThanSliceFromEmpty >(); + add< PushSliceTwoFields >(); + add< PushSliceAndNormal >(); + add< PushSliceTwoFieldsConflict >(); + add< PushSliceAndNormalConflict >(); + add< PushSliceInvalidEachType >(); + add< PushSliceInvalidSliceType >(); + add< PushSliceInvalidSliceValue >(); + add< PushSliceInvalidSliceDouble >(); + add< PushSliceValidSliceDouble >(); + add< PushSliceInvalidSlice >(); add< PushSortBelowFull >(); add< PushSortReachedFullExact >(); add< PushSortReachedFullWithBoth >(); add< PushSortToZero >(); add< PushSortToZeroFromNothing >(); add< PushSortFromNothing >(); - add< PushSortLongerThanTrimFromNothing >(); + add< PushSortLongerThanSliceFromNothing >(); add< PushSortFromEmpty >(); - add< PushSortLongerThanTrimFromEmpty >(); + add< PushSortLongerThanSliceFromEmpty >(); add< PushSortSortMixed >(); add< PushSortSortOutOfOrderFields >(); add< PushSortSortExtraFields >(); add< PushSortSortMissingFields >(); + add< PushSortSortNestedFields >(); + add< PushSortInvalidSortPattern >(); add< PushSortInvalidEachType >(); add< PushSortInvalidBaseArray >(); add< PushSortInvalidSortType >(); @@ -2519,9 +2599,9 @@ namespace UpdateTests { add< PushSortValidSortDouble >(); add< PushSortInvalidSortSort >(); add< PushSortInvalidSortSortOrder >(); - add< PushSortInvertedSortAndTrim >(); + add< PushSortInvertedSortAndSlice >(); add< PushSortInvalidDuplicatedSort >(); - add< PushSortInvalidMissingTrimTo >(); + add< PushSortInvalidMissingSliceTo >(); add< CantIncParent >(); add< DontDropEmpty >(); add< InsertInEmpty >(); @@ -2549,11 +2629,11 @@ namespace UpdateTests { add< ModSetTests::SetOnInsertRewriteExistingField >(); add< ModSetTests::SetOnInsertRewriteNonExistingField >(); add< ModSetTests::PushRewriteExistingField >(); - add< ModSetTests::PushTrimRewriteExistingField >(); + add< ModSetTests::PushSliceRewriteExistingField >(); add< ModSetTests::PushSortRewriteExistingField >(); add< ModSetTests::PushRewriteNonExistingField >(); - add< ModSetTests::PushTrimRewriteNonExistingField >(); - add< ModSetTests::PushTrimRewriteNested >(); + add< ModSetTests::PushSliceRewriteNonExistingField >(); + add< ModSetTests::PushSliceRewriteNested >(); add< ModSetTests::PushSortRewriteNonExistingField >(); add< ModSetTests::PushAllRewriteExistingField >(); add< ModSetTests::PushAllRewriteNonExistingField >(); |