How can I list all collections in the MongoDB shell?

You can do… JavaScript (shell): db.getCollectionNames() Node.js: db.listCollections() Non-JavaScript (shell only): show collections The reason I call that non-JavaScript is because: $ mongo prodmongo/app –eval “show collections” MongoDB shell version: 3.2.10 connecting to: prodmongo/app 2016-10-26T19:34:34.886-0400 E QUERY [thread1] SyntaxError: missing ; before statement @(shell eval):1:5 $ mongo prodmongo/app –eval “db.getCollectionNames()” MongoDB shell version: 3.2.10 connecting … Read more

Loop through all Mongo collections and execute query

There is the db.getCollectionNames() helper method that does this for you. You can then implement your code: db.getCollectionNames().forEach(function(collname) { // find the last item in a collection var last_element = db[collname].find().sort({_id:-1}).limit(1); // check that it’s not empty if (last_element.hasNext()) { // print its timestamp printjson(last_element.next()._id.getTimestamp()); } }) You probably also want a .hasNext() check in … Read more