1
// Basic js tests for the collMod command.
2
// Test setting the usePowerOf2Sizes flag, and modifying TTL indexes.
4
var coll = "collModTest";
5
var t = db.getCollection( coll );
8
db.createCollection( coll );
11
// Verify the new collection has userFlags set to 0
12
assert.eq( t.stats().userFlags , 0 , "fresh collection doesn't have userFlags = 0 ");
14
// Modify the collection with the usePowerOf2Sizes flag. Verify userFlags now = 1.
15
var res = db.runCommand( { "collMod" : coll, "usePowerOf2Sizes" : true } );
17
assert.eq( res.ok , 1 , "collMod failed" );
18
assert.eq( t.stats().userFlags , 1 , "modified collection should have userFlags = 1 ");
20
// Try to modify it with some unrecognized value
21
var res = db.runCommand( { "collMod" : coll, "unrecognized" : true } );
23
assert.eq( res.ok , 0 , "collMod shouldn't return ok with unrecognized value" );
26
t.ensureIndex( {a : 1}, { "expireAfterSeconds": 50 } )
27
assert.eq( 1, db.system.indexes.count( { key : {a:1}, expireAfterSeconds : 50 } ),
28
"TTL index not added" );
30
// try to modify it with a bad key pattern
31
var res = db.runCommand( { "collMod" : coll,
32
"index" : { "keyPattern" : "bad" , "expireAfterSeconds" : 100 } } );
34
assert.eq( 0 , res.ok , "mod shouldn't work with bad keypattern");
36
// try to modify it without expireAfterSeconds field
37
var res = db.runCommand( { "collMod" : coll,
38
"index" : { "keyPattern" : {a : 1} } } );
40
assert.eq( 0 , res.ok , "TTL mod shouldn't work without expireAfterSeconds");
42
// try to modify it with a non-numeric expireAfterSeconds field
43
var res = db.runCommand( { "collMod" : coll,
44
"index" : { "keyPattern" : {a : 1}, "expireAfterSeconds" : "100" } } );
46
assert.eq( 0 , res.ok , "TTL mod shouldn't work with non-numeric expireAfterSeconds");
48
// this time modifying should finally work
49
var res = db.runCommand( { "collMod" : coll,
50
"index" : { "keyPattern" : {a : 1}, "expireAfterSeconds" : 100 } } );
52
assert.eq( 1, db.system.indexes.count( { key : {a:1}, expireAfterSeconds : 100 } ),
53
"TTL index not modified" );
55
// try to modify a faulty TTL index with a non-numeric expireAfterSeconds field
56
t.dropIndex( {a : 1 } );
57
t.ensureIndex( {a : 1} , { "expireAfterSeconds": "50" } )
58
var res = db.runCommand( { "collMod" : coll,
59
"index" : { "keyPattern" : {a : 1} , "expireAfterSeconds" : 100 } } );
61
assert.eq( 0, res.ok, "shouldn't be able to modify faulty index spec" );
63
// try with new index, this time set both expireAfterSeconds and the usePowerOf2Sizes flag
64
t.dropIndex( {a : 1 } );
65
t.ensureIndex( {a : 1} , { "expireAfterSeconds": 50 } )
66
var res = db.runCommand( { "collMod" : coll ,
67
"usePowerOf2Sizes" : false,
68
"index" : { "keyPattern" : {a : 1} , "expireAfterSeconds" : 100 } } );
70
assert.eq( 1, res.ok, "should be able to modify both userFlags and expireAfterSeconds" );
71
assert.eq( t.stats().userFlags , 0 , "userflags should be 0 now");
72
assert.eq( 1, db.system.indexes.count( { key : {a:1}, expireAfterSeconds : 100 } ),
73
"TTL index should be 100 now" );