Skip to content

Instantly share code, notes, and snippets.

@break24
Last active April 10, 2019 11:14
Show Gist options
  • Save break24/ecdcb0649d3e0ee70870d2d138b99e9b to your computer and use it in GitHub Desktop.
Save break24/ecdcb0649d3e0ee70870d2d138b99e9b to your computer and use it in GitHub Desktop.
Mongo snippets
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 + '%' : '-')}})
db.getCollectionNames().forEach(function(item){ db[item].dropIndexes()})
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());}, []))
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);
});
});
});

Some snippets to analyze indexes for mongo shell or tools like NoSQLBooster

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment