This function lists all collections in a MongoDB database, with the number of documents in each collection (bluebird promises).
function listMongoCollections(db) {
var promises = []
db.listCollections().toArray().then((docs) => {
docs.forEach((doc) => {
promises.push(
new Promise((resolve) => {
db.collection(doc.name).count().then((count) => {
doc.count = count
resolve()
})
})
)
})
return Promise.all(promises)
})
}
Is there a simpler way to do this? Using this method will flood an app with code, and I haven't even included error handling.