How to check current setting of TTL

Hi. Who knows how to check the current setting of TTL?

Welcome to the MongoDB Community @Steven_Yu !

Time-to-Live (TTL) indexes are defined per-collection based on a provided expireAfterSeconds value. By default collections do not have a TTL index.

You can use the db.collection.getIndexes() helper in the MongoDB Shell to get index information, for example:

test> db.eventlog.createIndex(
    { "lastModifiedDate": 1 },
    { expireAfterSeconds: 7200 }
)
lastModifiedDate_1

test> db.eventlog.getIndexes()
[
  { v: 2, key: { _id: 1 }, name: '_id_' },
  {
    v: 2,
    key: { lastModifiedDate: 1 },
    name: 'lastModifiedDate_1',
    expireAfterSeconds: 7200
  }
]

You can modify the expiry for a TTL index using the collMod command, for example:

test> db.runCommand({
   'collMod': "eventlog", 
   index: {
      keyPattern: { "lastModifiedDate": 1 },
      expireAfterSeconds: 3600
   }
})
{
  expireAfterSeconds_old: Long("7200"),
  expireAfterSeconds_new: Long("3600"),
  ok: 1
}

If that isn’t the information you are looking for, please provide some further details on your environment:

  • MongoDB server version
  • Driver/tool version you are using to display TTL details
  • TTL information you require

Thanks,
Stennie