1
// Test simple updates issued through mongos. Updates have different constraints through mongos,
2
// since shard key is immutable.
1
4
s = new ShardingTest( "auto1" , 2 , 1 , 1 );
3
6
s.adminCommand( { enablesharding : "test" } );
4
s.adminCommand( { shardcollection : "test.update1" , key : { key : 1 } } );
7
// repeat same tests with hashed shard key, to ensure identical behavior
8
s.adminCommand( { shardcollection : "test.update0" , key : { key : 1 } } );
9
s.adminCommand( { shardcollection : "test.update1" , key : { key : "hashed" } } );
6
11
db = s.getDB( "test" )
9
coll.insert({_id:1, key:1});
11
// these are both upserts
12
coll.save({_id:2, key:2});
13
coll.update({_id:3, key:3}, {$set: {foo: 'bar'}}, {upsert: true});
15
assert.eq(coll.count(), 3, "count A")
16
assert.eq(coll.findOne({_id:3}).key, 3 , "findOne 3 key A")
17
assert.eq(coll.findOne({_id:3}).foo, 'bar' , "findOne 3 foo A")
19
// update existing using save()
20
coll.save({_id:1, key:1, other:1});
22
// update existing using update()
23
coll.update({_id:2}, {key:2, other:2});
24
coll.update({_id:3}, {key:3, other:3});
26
coll.update({_id:3, key:3}, {other:4});
27
assert.eq(db.getLastErrorObj().code, 12376, 'bad update error');
29
assert.eq(coll.count(), 3, "count B")
30
coll.find().forEach(function(x){
31
assert.eq(x._id, x.key, "_id == key");
32
assert.eq(x._id, x.other, "_id == other");
36
coll.update({_id:1, key:1}, {$set: {key:2}});
37
err = db.getLastErrorObj();
38
assert.eq(coll.findOne({_id:1}).key, 1, 'key unchanged');
39
assert.eq(err.code, 13123, 'key error code 1');
40
assert.eq(err.code, 13123, 'key error code 2');
42
coll.update({_id:1, key:1}, {$set: {foo:2}});
43
assert.isnull(db.getLastError(), 'getLastError reset');
45
coll.update( { key : 17 } , { $inc : { x : 5 } } , true );
46
assert.eq( 5 , coll.findOne( { key : 17 } ).x , "up1" )
48
coll.update( { key : 18 } , { $inc : { x : 5 } } , true , true );
49
assert.eq( 5 , coll.findOne( { key : 18 } ).x , "up2" )
13
coll = db.getCollection("update" + i);
15
coll.insert({_id:1, key:1});
17
// these are both upserts
18
coll.save({_id:2, key:2});
19
coll.update({_id:3, key:3}, {$set: {foo: 'bar'}}, {upsert: true});
21
assert.eq(coll.count(), 3, "count A")
22
assert.eq(coll.findOne({_id:3}).key, 3 , "findOne 3 key A")
23
assert.eq(coll.findOne({_id:3}).foo, 'bar' , "findOne 3 foo A")
25
// update existing using save()
26
coll.save({_id:1, key:1, other:1});
28
// update existing using update()
29
coll.update({_id:2}, {key:2, other:2});
30
coll.update({_id:3}, {key:3, other:3});
32
// do a replacement-style update which queries the shard key and keeps it constant
33
coll.save( {_id:4, key:4} );
34
coll.update({key:4}, {key:4, other:4});
35
assert.eq( coll.find({key:4, other:4}).count() , 1 , 'replacement update error');
36
coll.remove( {_id:4} )
38
assert.eq(coll.count(), 3, "count B")
39
coll.find().forEach(function(x){
40
assert.eq(x._id, x.key, "_id == key");
41
assert.eq(x._id, x.other, "_id == other");
45
coll.update({_id:1, key:1}, {$set: {key:2}});
46
err = db.getLastErrorObj();
47
assert.eq(coll.findOne({_id:1}).key, 1, 'key unchanged');
48
assert.eq(err.code, 13123, 'key error code 1');
49
assert.eq(err.code, 13123, 'key error code 2');
51
coll.update({_id:1, key:1}, {$set: {foo:2}});
52
assert.isnull(db.getLastError(), 'getLastError reset');
54
coll.update( { key : 17 } , { $inc : { x : 5 } } , true );
55
assert.eq( 5 , coll.findOne( { key : 17 } ).x , "up1" )
57
coll.update( { key : 18 } , { $inc : { x : 5 } } , true , true );
58
assert.eq( 5 , coll.findOne( { key : 18 } ).x , "up2" )