Está el db.getCollectionNames()
método de ayuda que hace esto por usted. A continuación, puede implementar su código:
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());
}
})
Probablemente también quieras un .hasNext()
regístrese allí para atender posibles colecciones vacías.