2012-12-24 17:30:16 -05:00
|
|
|
// Basic js tests for the collMod command.
|
|
|
|
|
// Test setting the usePowerOf2Sizes flag, and modifying TTL indexes.
|
|
|
|
|
|
2013-11-06 17:20:28 -05:00
|
|
|
function debug( x ) {
|
|
|
|
|
//printjson( x );
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-24 17:30:16 -05:00
|
|
|
var coll = "collModTest";
|
|
|
|
|
var t = db.getCollection( coll );
|
2012-12-24 20:11:05 -05:00
|
|
|
t.drop();
|
|
|
|
|
|
2015-02-17 18:20:42 -05:00
|
|
|
var isMongos = ("isdbgrid" == db.runCommand("ismaster").msg);
|
|
|
|
|
|
2012-12-24 20:11:05 -05:00
|
|
|
db.createCollection( coll );
|
|
|
|
|
|
2014-08-01 11:36:58 -04:00
|
|
|
function findTTL( key, expireAfterSeconds ) {
|
|
|
|
|
var all = t.getIndexes();
|
|
|
|
|
all = all.filter( function(z) {
|
|
|
|
|
return z.expireAfterSeconds == expireAfterSeconds &&
|
|
|
|
|
friendlyEqual( z.key, key ); } );
|
|
|
|
|
return all.length == 1;
|
|
|
|
|
}
|
2012-12-24 17:30:16 -05:00
|
|
|
|
2015-02-17 18:20:42 -05:00
|
|
|
function findCollectionInfo() {
|
|
|
|
|
var all = db.getCollectionInfos();
|
|
|
|
|
all = all.filter( function(z) { return z.name == t.getName(); } );
|
|
|
|
|
assert.eq(all.length, 1);
|
|
|
|
|
return all[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ensure we fail with gibberish options
|
|
|
|
|
assert.commandFailed(t.runCommand('collmod', {NotARealOption:1}));
|
|
|
|
|
|
2012-12-24 17:30:16 -05:00
|
|
|
// add a TTL index
|
|
|
|
|
t.ensureIndex( {a : 1}, { "expireAfterSeconds": 50 } )
|
2014-08-01 11:36:58 -04:00
|
|
|
assert( findTTL( { a : 1 }, 50 ), "TTL index not added" );
|
2012-12-24 17:30:16 -05:00
|
|
|
|
|
|
|
|
// try to modify it with a bad key pattern
|
|
|
|
|
var res = db.runCommand( { "collMod" : coll,
|
|
|
|
|
"index" : { "keyPattern" : "bad" , "expireAfterSeconds" : 100 } } );
|
2013-11-06 17:20:28 -05:00
|
|
|
debug( res );
|
2012-12-24 17:30:16 -05:00
|
|
|
assert.eq( 0 , res.ok , "mod shouldn't work with bad keypattern");
|
|
|
|
|
|
|
|
|
|
// try to modify it without expireAfterSeconds field
|
|
|
|
|
var res = db.runCommand( { "collMod" : coll,
|
|
|
|
|
"index" : { "keyPattern" : {a : 1} } } );
|
2013-11-06 17:20:28 -05:00
|
|
|
debug( res );
|
2012-12-24 17:30:16 -05:00
|
|
|
assert.eq( 0 , res.ok , "TTL mod shouldn't work without expireAfterSeconds");
|
|
|
|
|
|
|
|
|
|
// try to modify it with a non-numeric expireAfterSeconds field
|
|
|
|
|
var res = db.runCommand( { "collMod" : coll,
|
|
|
|
|
"index" : { "keyPattern" : {a : 1}, "expireAfterSeconds" : "100" } } );
|
2013-11-06 17:20:28 -05:00
|
|
|
debug( res );
|
2012-12-24 17:30:16 -05:00
|
|
|
assert.eq( 0 , res.ok , "TTL mod shouldn't work with non-numeric expireAfterSeconds");
|
|
|
|
|
|
|
|
|
|
// this time modifying should finally work
|
|
|
|
|
var res = db.runCommand( { "collMod" : coll,
|
|
|
|
|
"index" : { "keyPattern" : {a : 1}, "expireAfterSeconds" : 100 } } );
|
2013-11-06 17:20:28 -05:00
|
|
|
debug( res );
|
2014-08-01 11:36:58 -04:00
|
|
|
assert( findTTL( {a:1}, 100 ), "TTL index not modified" );
|
2012-12-24 17:30:16 -05:00
|
|
|
|
|
|
|
|
// try to modify a faulty TTL index with a non-numeric expireAfterSeconds field
|
|
|
|
|
t.dropIndex( {a : 1 } );
|
|
|
|
|
t.ensureIndex( {a : 1} , { "expireAfterSeconds": "50" } )
|
|
|
|
|
var res = db.runCommand( { "collMod" : coll,
|
|
|
|
|
"index" : { "keyPattern" : {a : 1} , "expireAfterSeconds" : 100 } } );
|
2013-11-06 17:20:28 -05:00
|
|
|
debug( res );
|
2012-12-24 17:30:16 -05:00
|
|
|
assert.eq( 0, res.ok, "shouldn't be able to modify faulty index spec" );
|
|
|
|
|
|
|
|
|
|
// try with new index, this time set both expireAfterSeconds and the usePowerOf2Sizes flag
|
|
|
|
|
t.dropIndex( {a : 1 } );
|
|
|
|
|
t.ensureIndex( {a : 1} , { "expireAfterSeconds": 50 } )
|
|
|
|
|
var res = db.runCommand( { "collMod" : coll ,
|
2014-01-06 23:53:21 -05:00
|
|
|
"usePowerOf2Sizes" : true,
|
2012-12-24 17:30:16 -05:00
|
|
|
"index" : { "keyPattern" : {a : 1} , "expireAfterSeconds" : 100 } } );
|
2013-11-06 17:20:28 -05:00
|
|
|
debug( res );
|
2014-08-01 11:36:58 -04:00
|
|
|
assert( findTTL( {a:1}, 100), "TTL index should be 100 now" );
|
2012-12-24 17:30:16 -05:00
|
|
|
|
2015-02-17 18:20:42 -05:00
|
|
|
// Clear usePowerOf2Sizes and enable noPadding. Make sure collection options.flags is updated.
|
|
|
|
|
var res = db.runCommand( { "collMod" : coll ,
|
|
|
|
|
"usePowerOf2Sizes" : false,
|
|
|
|
|
"noPadding" : true} )
|
|
|
|
|
debug( res );
|
|
|
|
|
assert.commandWorked(res);
|
|
|
|
|
var info = findCollectionInfo();
|
|
|
|
|
assert.eq(info.options.flags, 2, tojson(info)); // 2 is CollectionOptions::Flag_NoPadding
|
|
|
|
|
|
|
|
|
|
// Clear noPadding and check results object and options.flags.
|
|
|
|
|
var res = db.runCommand( { "collMod" : coll ,
|
|
|
|
|
"noPadding" : false} )
|
|
|
|
|
debug( res );
|
|
|
|
|
assert.commandWorked(res);
|
|
|
|
|
if (!isMongos) {
|
|
|
|
|
// don't check this for sharding passthrough since mongos has a different output format.
|
|
|
|
|
assert.eq(res.noPadding_old, true, tojson(res));
|
|
|
|
|
assert.eq(res.noPadding_new, false, tojson(res));
|
|
|
|
|
}
|
|
|
|
|
var info = findCollectionInfo();
|
|
|
|
|
assert.eq(info.options.flags, 0, tojson(info));
|