Loop through all Mongo collections and execute query

孤街醉人 提交于 2019-11-26 23:14:39

问题


First of, I'm quite new to mongodb. Here's my question I've not been able to find a solution to.

Let's say I have 3 different collections.

mongos> show collections
collectionA
collectionB
collectionC

I want to create a script that iterates over all collections ind this database and find the last inserted timestamp in each of these collections. Here's what works inside mongos.

var last_element = db.collectionA.find().sort({_id:-1}).limit(1);
printjson(last_element.next()._id.getTimestamp());
ISODate("2014-08-28T06:45:47Z")

1. Problem (Iterate over all collections)

Is there any possibility to to sth. like.

var my_collections = show collections;
my_collections.forEach(function(current_collection){
    print(current_collection);
});

Problem here, the assignment for my_collections does not work. I get SyntaxError: Unexpected identifier. Do I need to quote the 'show' statement ? Is it even possible ?

2. Problem (storing collection in js var)

I can workaround Problem 1 by doing this:

var my_collections = ["collectionA", "collectionB", "collectionC"];
my_collections.forEach(function(current_collection){
    var last_element = db.current_collection.find().sort({_id:-1}).limit(1);
    print(current_collection);
    printjson(last_element.next()._id.getTimestamp());
});

The last_element.next() produces the following error:

error hasNext: false at src/mongo/shell/query.js:124

It seems that last_element isn't saved correctly.

Any suggestions on what I'm doing wrong??


UPDATE

Neils answer lead me to this solution. In addition to his code I had to check if the function getTimestamp really exist. For some 'virtual' collections there seem to be no _id property.

db.getCollectionNames().forEach(function(collname) {
    var last_element = db[collname].find().sort({_id:-1}).limit(1);
    if(last_element.hasNext()){
        var next = last_element.next();
        if(next._id !== undefined && typeof next._id.getTimestamp == 'function'){
           printjson(collname + " >> "+next._id.getTimestamp());
        }else{
          print(collname + " undefined!! (getTimestamp N/A)")
        }
    }
});

回答1:


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.



来源:https://stackoverflow.com/questions/25543452/loop-through-all-mongo-collections-and-execute-query

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