jstests/libs/clustered_collections/clustered_capped_utils.js
// Validate TTL-based deletion on a clustered, capped collection.
Validate TTL-based deletion on a clustered, capped collection.
Validate TTL - based deletion on a clustered capped collection .
static testClusteredCappedCollectionWithTTL(db, collName, clusterKeyField) {
        jsTest.log("Validating TTL operation on capped clustered collection");
        const expireAfterSeconds = 60 * 60 * 24;
        const clusterKey = {[clusterKeyField]: 1};
        const coll = db[collName];
        const now = new Date();
        const batchSize = 10;
        const clusterKeyFieldName = Object.keys(clusterKey)[0];
        coll.drop();
        assert.commandWorked(db.createCollection(
            coll.getName(),
            {clusteredIndex: {key: clusterKey, unique: true}, capped: true, expireAfterSeconds}));
        let docs = [];
        for (let i = batchSize; i; i--) {
            const tenTimesExpiredMs = 10 * expireAfterSeconds * 1000;
            const pastDate = new Date(now - tenTimesExpiredMs - i);
            docs.push({
                [clusterKeyFieldName]: pastDate,
                info: "expired",
            });
        }
        assert.commandWorked(coll.insertMany(docs, {ordered: true}));
        docs = [];
        for (let i = batchSize; i; i--) {
            const recentDate = new Date(now - i);
            docs.push({
                [clusterKeyFieldName]: recentDate,
                info: "unexpired",
            });
        }
        assert.commandWorked(coll.insertMany(docs, {ordered: true}));
        ClusteredCollectionUtil.waitForTTL(db);
        assert.eq(coll.find().itcount(), batchSize);
        coll.drop();
    }
static testClusteredCappedCollectionWithTTL ( db , collName , clusterKeyField ) { jsTest . log ( "Validating TTL operation on capped clustered collection" ) ; const expireAfterSeconds = 60 * 60 * 24 ; const clusterKey = { [ clusterKeyField ] : 1 } ; const coll = db [ collName ] ; const now = new Date ( ) ; const batchSize = 10 ; const clusterKeyFieldName = Object . keys ( clusterKey ) [ 0 ] ; coll . drop ( ) ; assert . commandWorked ( db . createCollection ( coll . getName ( ) , { clusteredIndex : { key : clusterKey , unique : true } , capped : true , expireAfterSeconds } ) ) ; let docs = [ ] ; for ( let i = batchSize ; i ; i -- ) { const tenTimesExpiredMs = 10 * expireAfterSeconds * 1000 ; const pastDate = new Date ( now - tenTimesExpiredMs - i ) ; docs . push ( { [ clusterKeyFieldName ] : pastDate , info : "expired" , } ) ; } assert . commandWorked ( coll . insertMany ( docs , { ordered : true } ) ) ; docs = [ ] ; for ( let i = batchSize ; i ; i -- ) { const recentDate = new Date ( now - i ) ; docs . push ( { [ clusterKeyFieldName ] : recentDate , info : "unexpired" , } ) ; } assert . commandWorked ( coll . insertMany ( docs , { ordered : true } ) ) ; ClusteredCollectionUtil . waitForTTL ( db ) ; assert . eq ( coll . find ( ) . itcount ( ) , batchSize ) ; coll . drop ( ) ; }
Validate TTL-based deletion on a clustered, capped collection.
Validate TTL - based deletion on a clustered capped collection .
// Set expireAfterSeconds to a day to safely test that only expired documents are deleted. // Only the recent documents survived.