1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
t = db.update7;
t.drop();
function s(){
return t.find().sort( { _id : 1 } ).map( function(z){ return z.x; } );
}
t.save( { _id : 1 , x : 1 } );
t.save( { _id : 2 , x : 5 } );
assert.eq( "1,5" , s() , "A" );
t.update( {} , { $inc : { x : 1 } } );
assert.eq( "2,5" , s() , "B" );
t.update( { _id : 1 } , { $inc : { x : 1 } } );
assert.eq( "3,5" , s() , "C" );
t.update( { _id : 2 } , { $inc : { x : 1 } } );
assert.eq( "3,6" , s() , "D" );
t.update( {} , { $inc : { x : 1 } } , false , true );
assert.eq( "4,7" , s() , "E" );
t.update( {} , { $set : { x : 2 } } , false , true );
assert.eq( "2,2" , s() , "F" );
// non-matching in cursor
t.drop();
t.save( { _id : 1 , x : 1 , a : 1 , b : 1 } );
t.save( { _id : 2 , x : 5 , a : 1 , b : 2 } );
assert.eq( "1,5" , s() , "B1" );
t.update( { a : 1 } , { $inc : { x : 1 } } , false , true );
assert.eq( "2,6" , s() , "B2" );
t.update( { b : 1 } , { $inc : { x : 1 } } , false , true );
assert.eq( "3,6" , s() , "B3" );
t.update( { b : 3 } , { $inc : { x : 1 } } , false , true );
assert.eq( "3,6" , s() , "B4" );
t.ensureIndex( { a : 1 } );
t.ensureIndex( { b : 1 } );
t.update( { a : 1 } , { $inc : { x : 1 } } , false , true );
assert.eq( "4,7" , s() , "B5" );
t.update( { b : 1 } , { $inc : { x : 1 } } , false , true );
assert.eq( "5,7" , s() , "B6" );
t.update( { b : 3 } , { $inc : { x : 1 } } , false , true );
assert.eq( "5,7" , s() , "B7" );
t.update( { b : 2 } , { $inc : { x : 1 } } , false , true );
assert.eq( "5,8" , s() , "B7" );
// multi-key
t.drop();
t.save( { _id : 1 , x : 1 , a : [ 1 , 2 ] } );
t.save( { _id : 2 , x : 5 , a : [ 2 , 3 ] } );
assert.eq( "1,5" , s() , "C1" );
t.update( { a : 1 } , { $inc : { x : 1 } } , false , true );
assert.eq( "2,5" , s() , "C2" );
t.update( { a : 1 } , { $inc : { x : 1 } } , false , true );
assert.eq( "3,5" , s() , "C3" );
t.update( { a : 3 } , { $inc : { x : 1 } } , false , true );
assert.eq( "3,6" , s() , "C4" );
t.update( { a : 2 } , { $inc : { x : 1 } } , false , true );
assert.eq( "4,7" , s() , "C5" );
t.update( { a : { $gt : 0 } } , { $inc : { x : 1 } } , false , true );
assert.eq( "5,8" , s() , "C6" );
t.drop();
t.save( { _id : 1 , x : 1 , a : [ 1 , 2 ] } );
t.save( { _id : 2 , x : 5 , a : [ 2 , 3 ] } );
t.ensureIndex( { a : 1 } );
assert.eq( "1,5" , s() , "D1" );
t.update( { a : 1 } , { $inc : { x : 1 } } , false , true );
assert.eq( "2,5" , s() , "D2" );
t.update( { a : 1 } , { $inc : { x : 1 } } , false , true );
assert.eq( "3,5" , s() , "D3" );
t.update( { a : 3 } , { $inc : { x : 1 } } , false , true );
assert.eq( "3,6" , s() , "D4" );
t.update( { a : 2 } , { $inc : { x : 1 } } , false , true );
assert.eq( "4,7" , s() , "D5" );
t.update( { a : { $gt : 0 } } , { $inc : { x : 1 } } , false , true );
assert.eq( "5,8" , s() , "D6" );
t.update( { a : { $lt : 10 } } , { $inc : { x : -1 } } , false , true );
assert.eq( "4,7" , s() , "D7" );
// ---
t.save( { _id : 3 } );
assert.eq( "4,7," , s() , "E1" );
t.update( {} , { $inc : { x : 1 } } , false , true );
assert.eq( "5,8,1" , s() , "E2" );
for ( i = 4; i<8; i++ )
t.save( { _id : i } );
t.save( { _id : i , x : 1 } );
assert.eq( "5,8,1,,,,,1" , s() , "E4" );
t.update( {} , { $inc : { x : 1 } } , false , true );
assert.eq( "6,9,2,1,1,1,1,2" , s() , "E5" );
// --- $inc indexed field
t.drop();
t.save( { x : 1 } );
t.save( { x : 2 } );
t.save( { x : 3 } );
t.ensureIndex( { x : 1 } );
assert.eq( "1,2,3" , s() , "F1" )
t.update( { x : { $gt : 0 } } , { $inc : { x : 5 } } , false , true );
assert.eq( "6,7,8" , s() , "F1" )
|