Some snippets to analyze indexes for mongo shell or tools like NoSQLBooster
Last active
April 10, 2019 11:14
-
-
Save break24/ecdcb0649d3e0ee70870d2d138b99e9b to your computer and use it in GitHub Desktop.
Mongo snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.currentOp() | |
.inprog.filter(item => item.query.createIndexes) | |
.map(item => { | |
return { | |
collection: item.query.createIndexes, | |
indexes: item.query.indexes.map(i => i.name).join(', '), | |
secs_running:item.secs_running, | |
progress: (item.progress ? Math.floor(item.progress.done/item.progress.total*1000)/10 + '%' : '-') | |
} | |
}) | |
db.currentOp().inprog.filter(item => item.query.createIndexes).map(item => {return {collection: item.query.createIndexes,indexes: item.query.indexes.map(i => i.name).join(', '),secs_running:item.secs_running,progress: (item.progress ? Math.floor(item.progress.done/item.progress.total*1000)/10 + '%' : '-')}}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.getCollectionNames().forEach(function(item){ db[item].dropIndexes()}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.getCollectionNames().reduce(function(a, item){ return a.concat(db[item].getIndexes());}, []) | |
// JSON.stringify(db.getCollectionNames().reduce(function(a, item){ return a.concat(db[item].getIndexes());}, [])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.adminCommand('listDatabases').databases.forEach(function(e){ | |
if ((e.name === "admin" || e.name === "config" || e.name === "local")) return; | |
var database=e.name; | |
var context=db.getSiblingDB(database); | |
context.getCollectionNames().forEach(function(collection){ | |
var records=context.getCollection(collection).aggregate( | |
[ | |
{ $indexStats: { } }, | |
{ | |
"$group" : | |
{ | |
_id : { name: "$name"}, | |
accesses:{$sum:"$accesses.ops"}, | |
since:{$min:"$accesses.since"}, | |
} | |
}, | |
{ | |
"$project": | |
{ | |
_id:0, | |
name:"$_id.name", | |
since:{ $dateToString: { format: "%Y-%m-%d-%H:%M:%S", date: "$since" } }, | |
accesses:"$accesses", | |
} | |
} | |
] | |
).forEach(function(index){ | |
var idx=index.name; | |
var since=index.since; | |
var accesses=index.accesses; | |
print(database+";"+collection+";"+idx+";"+since+";"+accesses); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment