Hello,
The snippet you shared works for hashed indexes as it searches for “hashed” in the “key” field, while the attribute you are searching for “expireAfterSeconds” for TTL indexes is a top level field.
I modified the snippet you shared to search for indexes that has the field “expireAfterSeconds” and returning only those.
I tested it in my environment and confirmed it returns only TTL indexes:
db.adminCommand("listDatabases").databases.forEach(function(d){
let mdb = db.getSiblingDB(d.name);
mdb.getCollectionInfos({ type: "collection" }).forEach(function(c){
let currentCollection = mdb.getCollection(c.name);
currentCollection.getIndexes().forEach(function(idx){
if(idx.expireAfterSeconds){
printjson(idx);
}
});
});
});
I hope you find this helpful.