File: findandmodify1.js

package info (click to toggle)
mongodb 1%3A2.4.10-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 82,464 kB
  • sloc: cpp: 740,225; ansic: 152,098; sh: 13,820; python: 11,864; makefile: 1,012; perl: 922; pascal: 617; java: 452; lisp: 222; asm: 174
file content (58 lines) | stat: -rw-r--r-- 1,970 bytes parent folder | download | duplicates (3)
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
s = new ShardingTest( "find_and_modify_sharded" , 2 , 2);

s.adminCommand( { enablesharding : "test" } );
db = s.getDB( "test" );
primary = s.getServer( "test" ).getDB( "test" );
secondary = s.getOther( primary ).getDB( "test" );

numObjs = 20;

// Turn balancer off - with small numbers of chunks the balancer tries to correct all imbalances, not just < 8
s.s.getDB( "config" ).settings.update({ _id : "balancer" }, { $set : { stopped : true } }, true )

s.adminCommand( { shardcollection : "test.stuff"  , key : {_id:1} } );

// pre-split the collection so to avoid interference from balancer
s.adminCommand( { split: "test.stuff" , middle : { _id : numObjs/2 } } );
s.adminCommand( { movechunk : "test.stuff" , find : { _id : numObjs/2 } , to : secondary.getMongo().name } ) ;

for (var i=0; i < numObjs; i++){
    db.stuff.insert({_id: i});
}
db.getLastError()

// put two docs in each chunk (avoid the split in 0, since there are no docs less than 0)
for (var i=2; i < numObjs; i+=2){
    if (i == numObjs/2)
        continue;
    s.adminCommand( { split: "test.stuff"  , middle : {_id: i} } );
}

s.printChunks();
assert.eq( numObjs/2, s.config.chunks.count(), "split failed" );
assert.eq( numObjs/4, s.config.chunks.count({ shard: "shard0000" }) );
assert.eq( numObjs/4, s.config.chunks.count({ shard: "shard0001" }) ); 

// update
for (var i=0; i < numObjs; i++){
    assert.eq(db.stuff.count({b:1}), i, "2 A");

    var out = db.stuff.findAndModify({query: {_id:i, b:null}, update: {$set: {b:1}}});
    assert.eq(out._id, i, "2 E");

    assert.eq(db.stuff.count({b:1}), i+1, "2 B");
}

// remove
for (var i=0; i < numObjs; i++){
    assert.eq(db.stuff.count(), numObjs - i, "3 A");
    assert.eq(db.stuff.count({_id: i}), 1, "3 B");

    var out = db.stuff.findAndModify({remove: true, query: {_id:i}});

    assert.eq(db.stuff.count(), numObjs - i - 1, "3 C");
    assert.eq(db.stuff.count({_id: i}), 0, "3 D");
    assert.eq(out._id, i, "3 E");
}

s.stop();