Skip to content

Instantly share code, notes, and snippets.

@philpoore
Created February 11, 2021 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philpoore/856dcfb483ace80e28326d1bbd0812d8 to your computer and use it in GitHub Desktop.
Save philpoore/856dcfb483ace80e28326d1bbd0812d8 to your computer and use it in GitHub Desktop.
MongoDB handy scripts
//////////////////////////////
// Get currently running ops, good for finding out indexing progress
db.currentOp().inprog.map(a => a.msg)
/* [
undefined,
undefined,
"Index Build: scanning collection Index Build: scanning collection: 10385246/54469342 19%",
] */
//////////////////////////////
// Get index names for given collection
db.collection.getIndexes().map(a => a.name)
//////////////////////////////
// Collection stats
const p = (a, s, lr = false) => ('' + a).pad(s, lr)
let collectionStats = () => {
print(
p('collection', 30, true),
p('count', 12),
p('size', 12),
p('storageSize', 12),
p('indexSize', 12),
p('numIndexes', 12)
)
print('-'.repeat(95))
db.getCollectionNames().forEach((collection) => {
const stats = db[collection].stats(1024**3)
print(
p(collection, 30, true),
p(stats.count, 12),
p(stats.size, 12),
p(stats.storageSize, 12),
p(stats.totalIndexSize, 12),
p(stats.nindexes, 12)
)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment