循环遍历所有 Mongo 集合并执行查询 [英] Loop through all Mongo collections and execute query

查看:15
本文介绍了循环遍历所有 Mongo 集合并执行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我对 mongodb 很陌生.这是我一直无法找到解决方案的问题.

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

假设我有 3 个不同的集合.

Let's say I have 3 different collections.

mongos> show collections
collectionA
collectionB
collectionC

我想创建一个脚本来迭代这个数据库中的所有集合,并在每个集合中找到最后插入的时间戳.以下是 mongos 的工作原理.

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.问题(迭代所有集合)

有没有可能……喜欢.

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

问题在这里,my_collections 的赋值不起作用.我得到 SyntaxError: Unexpected identifier.我需要引用'show'声明吗?甚至有可能吗?

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.问题(在 js var 中存储集合)

我可以通过这样做来解决问题 1:

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());
});

last_element.next() 产生以下错误:

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

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

last_element 似乎没有正确保存.

It seems that last_element isn't saved correctly.

对我做错了什么有什么建议吗??

Any suggestions on what I'm doing wrong??

更新

Neils 的回答让我找到了这个解决方案.除了他的代码之外,我还必须检查函数 getTimestamp 是否真的存在.对于某些虚拟"集合,似乎没有 _id 属性.

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)")
        }
    }
});

推荐答案

db.getCollectionNames() 辅助方法可以为您执行此操作.然后你可以实现你的代码:

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());
    }
})

您可能还需要一个 .hasNext() 签入以处理可能的空集合.

You probably also want a .hasNext() check in there to cater for possible empty collections.

这篇关于循环遍历所有 Mongo 集合并执行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆