Loop through all Mongo collections and execute query

孤街醉人 提交于 2019-11-27 23:31:47
Neil Lunn

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 there to cater for possible empty collections.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!