Summary
The script suggested in https://www.mongodb.com/docs/v5.0/tutorial/expire-data/#indexes-configured-using-nan is not working.
Motivation
Who is the affected end user?
All users who try to run the script are affected. If you run as-is you will see the error:
replset [direct: primary] percona> getNaNIndexes();
|
MongoServerError: Namespace local.system.tenantMigration.oplogView is a view, not a collection
|
How does this affect the end user?
Users cannot check their TTL index has any NaN value.
Suggested Fix
function getNaNIndexes() {
|
const nan_index = []; const dbs = db.adminCommand({ listDatabases: 1 }).databases; dbs.forEach((d) => {
|
if (d.name != 'local'){
|
const listCollCursor = db
|
.getSiblingDB(d.name)
|
.runCommand({ listCollections: 1 }).cursor; const collDetails = {
|
db: listCollCursor.ns.split(".$cmd")[0],
|
colls: listCollCursor.firstBatch.map((c) => c.name),
|
}; collDetails.colls.forEach((c) =>
|
db
|
.getSiblingDB(collDetails.db)
|
.getCollection(c)
|
.getIndexes()
|
.forEach((entry) => {
|
if (Object.is(entry.expireAfterSeconds, NaN)) {
|
nan_index.push({ ns: `${collDetails.db}.${c}`, index: entry });
|
}
|
})
|
);
|
}
|
}); return nan_index;
|
};getNaNIndexes();
|