Skip to content

Instantly share code, notes, and snippets.

@sar-joshi
Created January 21, 2025 06:37
Show Gist options
  • Save sar-joshi/65817ad1a50a9aea8ed5c046c87967c4 to your computer and use it in GitHub Desktop.
Save sar-joshi/65817ad1a50a9aea8ed5c046c87967c4 to your computer and use it in GitHub Desktop.
print("Database,Collection,Index Name,Is Unique,Is Sparse,TTL (seconds),Fields");
db.getMongo().getDBs().databases.forEach(function(database) {
// print("\nDatabase:", database.name);
if (database.name !== 'admin' && database.name !== 'local' && database.name !== 'config') {
var dbs = db.getSiblingDB(database.name);
dbs.getCollectionNames().forEach(function(collName) {
// print("\collName:", collName);
var indexes = db.getCollection(collName).getIndexes();
indexes.forEach(function(index) {
// Format fields with their sort direction
var fields = [];
for (var field in index.key) {
var direction = index.key[field];
var directionText = direction === 1 ? "ASC" :
direction === -1 ? "DESC" :
direction === "text" ? "TEXT" : direction;
fields.push(`${field} (${directionText})`);
}
// Create CSV row
var row = [
`"${database.name}"`,
`"${collName}"`,
`"${index.name}"`,
index.unique || false,
index.sparse || false,
index.expireAfterSeconds || '',
`"${fields.join(', ')}"`
];
print(row.join(','));
});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment